Best Mockito code snippet using org.mockito.stubbing.Stubber
Source:BDDMockito.java
...5package org.mockito;67import org.mockito.stubbing.Answer;8import org.mockito.stubbing.OngoingStubbing;9import org.mockito.stubbing.Stubber;1011/**12 * Behavior Driven Development style of writing tests uses <b>//given //when //then</b> comments as fundamental parts of your test methods.13 * This is exactly how we write our tests and we warmly encourage you to do so!14 * <p>15 * Start learning about BDD here: <a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">http://en.wikipedia.org/wiki/Behavior_Driven_Development</a>16 * <p>17 * The problem is that current stubbing api with canonical role of <b>when</b> word does not integrate nicely with <b>//given //when //then</b> comments.18 * It's because stubbing belongs to <b>given</b> component of the test and not to the <b>when</b> component of the test. 19 * Hence {@link BDDMockito} class introduces an alias so that you stub method calls with {@link BDDMockito#given(Object)} method. 20 * Now it really nicely integrates with the <b>given</b> component of a BDD style test! 21 * <p>22 * Here is how the test might look like: 23 * <pre>24 * import static org.mockito.BDDMockito.*;25 * 26 * Seller seller = mock(Seller.class);27 * Shop shop = new Shop(seller);28 * 29 * public void shouldBuyBread() throws Exception {30 * //given 31 * given(seller.askForBread()).willReturn(new Bread());32 * 33 * //when34 * Goods goods = shop.buyBread();35 * 36 * //then37 * assertThat(goods, containBread());38 * } 39 * </pre>40 * 41 * Stubbing voids with throwables:42 * <pre>43 * //given44 * willThrow(new RuntimeException("boo")).given(mock).foo();45 * 46 * //when47 * Result result = systemUnderTest.perform();48 * 49 * //then50 * assertEquals(failure, result);51 * </pre>52 * <p>53 * One of the purposes of BDDMockito is also to show how to tailor the mocking syntax to a different programming style. 54 */55@SuppressWarnings("unchecked")56public class BDDMockito extends Mockito {57 58 /**59 * See original {@link OngoingStubbing}60 */61 public static interface BDDMyOngoingStubbing<T> {62 63 /**64 * See original {@link OngoingStubbing#thenAnswer(Answer)}65 */66 BDDMyOngoingStubbing<T> willAnswer(Answer<?> answer);67 68 /**69 * See original {@link OngoingStubbing#thenReturn(Object)}70 */71 BDDMyOngoingStubbing<T> willReturn(T value);72 73 /**74 * See original {@link OngoingStubbing#thenReturn(Object, Object...)}75 */76 BDDMyOngoingStubbing<T> willReturn(T value, T... values);77 78 /**79 * See original {@link OngoingStubbing#thenThrow(Throwable...)}80 */81 BDDMyOngoingStubbing<T> willThrow(Throwable... throwables);8283 /**84 * See original {@link OngoingStubbing#thenCallRealMethod()}85 */86 BDDMyOngoingStubbing<T> willCallRealMethod();87 }88 89 public static class BDDOngoingStubbingImpl<T> implements BDDMyOngoingStubbing<T> {9091 private final OngoingStubbing<T> mockitoOngoingStubbing;9293 public BDDOngoingStubbingImpl(OngoingStubbing<T> ongoingStubbing) {94 this.mockitoOngoingStubbing = ongoingStubbing;95 }9697 /* (non-Javadoc)98 * @see org.mockitousage.customization.BDDMockito.BDDMyOngoingStubbing#willAnswer(org.mockito.stubbing.Answer)99 */100 public BDDMyOngoingStubbing<T> willAnswer(Answer<?> answer) {101 return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenAnswer(answer));102 }103104 /* (non-Javadoc)105 * @see org.mockitousage.customization.BDDMockito.BDDMyOngoingStubbing#willReturn(java.lang.Object)106 */107 public BDDMyOngoingStubbing<T> willReturn(T value) {108 return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenReturn(value));109 }110111 /* (non-Javadoc)112 * @see org.mockitousage.customization.BDDMockito.BDDMyOngoingStubbing#willReturn(java.lang.Object, T[])113 */114 public BDDMyOngoingStubbing<T> willReturn(T value, T... values) {115 return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenReturn(value, values));116 }117118 /* (non-Javadoc)119 * @see org.mockitousage.customization.BDDMockito.BDDMyOngoingStubbing#willThrow(java.lang.Throwable[])120 */121 public BDDMyOngoingStubbing<T> willThrow(Throwable... throwables) {122 return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenThrow(throwables));123 }124125 public BDDMyOngoingStubbing<T> willCallRealMethod() {126 return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenCallRealMethod());127 }128 }129 130 /**131 * see original {@link Mockito#when(Object)}132 */133 public static <T> BDDMyOngoingStubbing<T> given(T methodCall) {134 return new BDDOngoingStubbingImpl<T>(Mockito.when(methodCall));135 }136 137 /**138 * See original {@link Stubber}139 */140 public static interface BDDStubber {141 /**142 * See original {@link Stubber#doAnswer(Answer)}143 */144 BDDStubber willAnswer(Answer answer);145 146 /**147 * See original {@link Stubber#doNothing()}148 */149 BDDStubber willNothing();150 151 /**152 * See original {@link Stubber#doReturn(Object)}153 */154 BDDStubber willReturn(Object toBeReturned);155 156 /**157 * See original {@link Stubber#doThrow(Throwable)}158 */159 BDDStubber willThrow(Throwable toBeThrown);160 161 /**162 * See original {@link Stubber#when(Object)}163 */164 <T> T given(T mock);165 }166 167 public static class BDDStubberImpl implements BDDStubber {168169 private final Stubber mockitoStubber;170171 public BDDStubberImpl(Stubber mockitoStubber) {172 this.mockitoStubber = mockitoStubber;173 }174175 /* (non-Javadoc)176 * @see org.mockitousage.customization.BDDMockito.BDDStubber#given(java.lang.Object)177 */178 public <T> T given(T mock) {179 return mockitoStubber.when(mock);180 }181182 /* (non-Javadoc)183 * @see org.mockitousage.customization.BDDMockito.BDDStubber#willAnswer(org.mockito.stubbing.Answer)184 */185 public BDDStubber willAnswer(Answer answer) {186 return new BDDStubberImpl(mockitoStubber.doAnswer(answer));187 }188189 /* (non-Javadoc)190 * @see org.mockitousage.customization.BDDMockito.BDDStubber#willNothing()191 */192 public BDDStubber willNothing() {193 return new BDDStubberImpl(mockitoStubber.doNothing());194 }195196 /* (non-Javadoc)197 * @see org.mockitousage.customization.BDDMockito.BDDStubber#willReturn(java.lang.Object)198 */199 public BDDStubber willReturn(Object toBeReturned) {200 return new BDDStubberImpl(mockitoStubber.doReturn(toBeReturned));201 }202203 /* (non-Javadoc)204 * @see org.mockitousage.customization.BDDMockito.BDDStubber#willThrow(java.lang.Throwable)205 */206 public BDDStubber willThrow(Throwable toBeThrown) {207 return new BDDStubberImpl(mockitoStubber.doThrow(toBeThrown));208 }209 }210 211 /**212 * see original {@link Mockito#doThrow(Throwable)}213 */214 public static BDDStubber willThrow(Throwable toBeThrown) {215 return new BDDStubberImpl(Mockito.doThrow(toBeThrown));216 }217 218 /**219 * see original {@link Mockito#doAnswer(Answer)}220 */221 public static BDDStubber willAnswer(Answer answer) {222 return new BDDStubberImpl(Mockito.doAnswer(answer));223 } 224 225 /**226 * see original {@link Mockito#doNothing()}227 */228 public static BDDStubber willDoNothing() {229 return new BDDStubberImpl(Mockito.doNothing());230 } 231 232 /**233 * see original {@link Mockito#doReturn(Object)}234 */235 public static BDDStubber willReturn(Object toBeReturned) {236 return new BDDStubberImpl(Mockito.doReturn(toBeReturned));237 }238239 /**240 * see original {@link Mockito#doCallRealMethod()}241 */242 public static BDDStubber willCallRealMethod() {243 return new BDDStubberImpl(Mockito.doCallRealMethod());244 }
...
Source:DefaultLenientStubber.java
...5package org.mockito.internal.stubbing;6import org.mockito.internal.MockitoCore;7import org.mockito.quality.Strictness;8import org.mockito.stubbing.Answer;9import org.mockito.stubbing.LenientStubber;10import org.mockito.stubbing.OngoingStubbing;11import org.mockito.stubbing.Stubber;12public class DefaultLenientStubber implements LenientStubber {13 private static final MockitoCore MOCKITO_CORE = new MockitoCore();14 @Override15 public Stubber doThrow(Throwable... toBeThrown) {16 return stubber().doThrow(toBeThrown);17 }18 @Override19 public Stubber doThrow(Class<? extends Throwable> toBeThrown) {20 return stubber().doThrow(toBeThrown);21 }22 @Override23 public Stubber doThrow(24 Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... nextToBeThrown) {25 return stubber().doThrow(toBeThrown, nextToBeThrown);26 }27 @Override28 public Stubber doAnswer(Answer answer) {29 return stubber().doAnswer(answer);30 }31 @Override32 public Stubber doNothing() {33 return stubber().doNothing();34 }35 @Override36 public Stubber doReturn(Object toBeReturned) {37 return stubber().doReturn(toBeReturned);38 }39 @Override40 public Stubber doReturn(Object toBeReturned, Object... nextToBeReturned) {41 return stubber().doReturn(toBeReturned, nextToBeReturned);42 }43 @Override44 public Stubber doCallRealMethod() {45 return stubber().doCallRealMethod();46 }47 @Override48 public <T> OngoingStubbing<T> when(T methodCall) {49 OngoingStubbingImpl<T> ongoingStubbing =50 (OngoingStubbingImpl) MOCKITO_CORE.when(methodCall);51 ongoingStubbing.setStrictness(Strictness.LENIENT);52 return ongoingStubbing;53 }54 private static Stubber stubber() {55 return MOCKITO_CORE.stubber(Strictness.LENIENT);56 }57}...
Stubber
Using AI Code Generation
1import org.mockito.stubbing.Stubber;2import org.mockito.invocation.InvocationOnMock;3import org.mockito.stubbing.Answer;4import org.mockito.stubbing.OngoingStubbing;5import org.mockito.InOrder;6import org.mockito.stubbing.Stubber;7import org.mockito.stubbing.Answer;8import org.mockito.stubbing.Answer;9import
Stubber
Using AI Code Generation
1import org.mockito.stubbing.Stubber;2import org.mockito.stubbing.Answer;3import org.mockito.invocation.InvocationOnMock;4import org.mockito.stubbing.OngoingStubbing;5import org.mockito.stubbing.Stubber;6{7 public static void main(String[] args)8 {9 List<String> mockList = mock(List.class);10 Stubber stubber = doReturn("Hello").when(mockList);11 stubber.get(0);12 System.out.println(mockList.get(0));13 }14}
Stubber
Using AI Code Generation
1import org.mockito.stubbing.Stubber;2import org.mockito.stubbing.Answer;3import org.mockito.stubbing.OngoingStubbing;4import org.mockito.stubbing.Stubber;5import org.mockito.stubbing.Stubber;6public class StubberTest {7 public static void main(String[] args) {8 List mockList = mock(List.class);9 Stubber stubber = doReturn("Mockito").when(mockList).get(0);10 stubber.thenReturn("rocks");11 System.out.println(mockList.get(0));12 System.out.println(mockList.get(0));13 }14}
Stubber
Using AI Code Generation
1package org.mockito.stubbing;2import org.mockito.Mockito;3public class Stubber {4 private final Mockito mockito;5 private final Object mock;6 public Stubber(Mockito mockito, Object mock) {7 this.mockito = mockito;8 this.mock = mock;9 }10 public <T> OngoingStubbing<T> when(T methodCall) {11 return mockito.when(methodCall);12 }13}14package org.mockito;15import org.mockito.stubbing.Stubber;16public class Mockito {17 public static <T> OngoingStubbing<T> when(T methodCall) {18 return new OngoingStubbing<T>();19 }20}21package org.mockito.stubbing;22public class OngoingStubbing<T> {23 public OngoingStubbing<T> thenReturn(T value) {24 return this;25 }26}27package org.mockito.stubbing;28import org.junit.Test;29public class MockitoTest {30 public void test() {31 Object mock = new Object();32 Mockito mockito = new Mockito();33 Stubber stubber = new Stubber(mockito, mock);34 OngoingStubbing<Object> ongoingStubbing = stubber.when(mock);35 ongoingStubbing.thenReturn(new Object());36 }37}
Stubber
Using AI Code Generation
1package org.mockito.stubbing;2import org.mockito.Mockito;3import org.mockito.stubbing.Stubber;4import java.util.List;5public class StubberClass {6 public static void main(String[] args) {7 List mockedList = Mockito.mock(List.class);8 Stubber stubber = Mockito.doThrow(new RuntimeException()).doNothing().doReturn("foo");9 stubber.when(mockedList).get(0);10 System.out.println(mockedList.get(0));11 System.out.println(mockedList.get(0));12 }13}14Related posts: Mockito – doReturn() method with Stubber class Mockito – doAnswer() method with Stubber class Mockito – doThrow() method with Stubber class Mockito – doNothing() method with Stubber class Mockito – doCallRealMethod() method with Stubber class Mockito – doNothing() method Mockito – doThrow() method Mockito – doCallRealMethod() method Mockito – doAnswer() method Mockito – doReturn() method Mockito – Stubbing void methods with Exceptions Mockito – Stubbing void methods with Answers Mockito – Stubbing void methods with doThrow() Mockito – Stubbing void methods with doAnswer() Mockito – Stubbing void methods with doNothing() Mockito – Stubbing void methods with doCallRealMethod() Mockito – Stubbing void methods with
Stubber
Using AI Code Generation
1import static org.mockito.Mockito.*;2import org.mockito.stubbing.*;3import org.mockito.invocation.*;4import org.mockito.stubbing.Stubber;5import org.mockito.stubbing.Answer;6import org.mockito.stubbing.Stubber;7public class 1 {8 public static void main(String[] args) {9 List<String> mockedList = mock(List.class);10 Stubber stubber = doReturn("Hello").doReturn("World").when(mockedList);11 stubber.get(0);12 stubber.get(1);13 verify(mockedList, times(2)).get(anyInt());14 }15}16import static org.mockito.Mockito.*;17import org.mockito.stubbing.*;18import org.mockito.invocation.*;19import org.mockito.stubbing.Stubber;20import org.mockito.stubbing.Answer;21import org.mockito.stubbing.Stubber;22public class 2 {23 public static void main(String[] args) {24 List<String> mockedList = mock(List.class);25 Stubber stubber = doReturn("Hello").doReturn("World").when(mockedList);26 stubber.get(0);27 stubber.get(1);28 verify(mockedList, times(2)).get(anyInt());29 }30}31import static org.mockito.Mockito.*;32import org.mockito.stubbing.*;33import org.mockito.invocation.*;34import org.mockito.stubbing.Stubber;35import org.mockito.stubbing.Answer;36import org.mockito.stubbing.Stubber;37public class 3 {38 public static void main(String[] args) {39 List<String> mockedList = mock(List.class);40 Stubber stubber = doReturn("Hello").doReturn("World").when(mockedList);41 stubber.get(0);42 stubber.get(1);43 verify(mockedList, times(2)).get(anyInt());44 }45}
Stubber
Using AI Code Generation
1import org.mockito.stubbing.Stubber;2import org.mockito.Mockito;3import static org.mockito.Mockito.*;4import static org.junit.Assert.*;5import java.util.*;6import org.junit.Test;7public class StubberDemo {8 public void testStubber() {9 List mockedList = mock(List.class);10 Stubber stubber = doReturn("one").doReturn("two").when(mockedList);11 assertEquals("one", mockedList.get(0));12 assertEquals("two", mockedList.get(0));13 }14}
Stubber
Using AI Code Generation
1import org.mockito.stubbing.Stubber;2import static org.mockito.Mockito.*;3import org.mockito.Mock;4import org.mockito.MockitoAnnotations;5import org.junit.Test;6import org.junit.Before;7import static org.junit.Assert.assertEquals;8public class StubberExample {9 Calculator calc;10 public void setUp(){11 MockitoAnnotations.initMocks(this);12 }13 public void testStubber(){14 Stubber stubber = doReturn(5);15 stubber.when(calc).add(10,20);16 assertEquals(5,calc.add(10,20));17 assertEquals(0,calc.add(0,0));18 }19}
Stubber
Using AI Code Generation
1import static org.mockito.Mockito.*;2import org.mockito.stubbing.*;3import java.util.*;4class TestClass{5 public static void main(String args[]){6 List list = mock(List.class);7 Stubber stubber = doReturn("Hello").when(list);8 stubber.withArguments("World");9 System.out.println(list.get(0));10 System.out.println(list.get("World"));11 }12}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!