• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockitousage.customization;
6 
7 import org.assertj.core.api.Assertions;
8 import org.junit.Test;
9 import org.mockito.InOrder;
10 import org.mockito.Mock;
11 import org.mockito.exceptions.misusing.NotAMockException;
12 import org.mockito.exceptions.verification.NoInteractionsWanted;
13 import org.mockito.exceptions.verification.VerificationInOrderFailure;
14 import org.mockito.exceptions.verification.WantedButNotInvoked;
15 import org.mockito.invocation.InvocationOnMock;
16 import org.mockito.stubbing.Answer;
17 import org.mockitousage.IMethods;
18 import org.mockitousage.MethodsImpl;
19 import org.mockitoutil.TestBase;
20 
21 import java.util.Set;
22 
23 import static org.junit.Assert.fail;
24 import static org.mockito.BDDMockito.*;
25 
26 public class BDDMockitoTest extends TestBase {
27 
28     @Mock
29     IMethods mock;
30 
31     @Test
should_stub()32     public void should_stub() throws Exception {
33         given(mock.simpleMethod("foo")).willReturn("bar");
34 
35         Assertions.assertThat(mock.simpleMethod("foo")).isEqualTo("bar");
36         Assertions.assertThat(mock.simpleMethod("whatever")).isEqualTo(null);
37     }
38 
39     @Test
should_stub_with_throwable()40     public void should_stub_with_throwable() throws Exception {
41         given(mock.simpleMethod("foo")).willThrow(new SomethingWasWrong());
42 
43         try {
44             Assertions.assertThat(mock.simpleMethod("foo")).isEqualTo("foo");
45             fail();
46         } catch (SomethingWasWrong expected) {
47         }
48     }
49 
50     @Test
should_stub_with_throwable_class()51     public void should_stub_with_throwable_class() throws Exception {
52         given(mock.simpleMethod("foo")).willThrow(SomethingWasWrong.class);
53 
54         try {
55             Assertions.assertThat(mock.simpleMethod("foo")).isEqualTo("foo");
56             fail();
57         } catch (SomethingWasWrong expected) {
58         }
59     }
60 
61     @Test
62     @SuppressWarnings("unchecked")
should_stub_with_throwable_classes()63     public void should_stub_with_throwable_classes() throws Exception {
64         // unavoidable 'unchecked generic array creation' warning (from JDK7 onward)
65         given(mock.simpleMethod("foo")).willThrow(SomethingWasWrong.class, AnotherThingWasWrong.class);
66 
67         try {
68             Assertions.assertThat(mock.simpleMethod("foo")).isEqualTo("foo");
69             fail();
70         } catch (SomethingWasWrong expected) {
71         }
72     }
73 
74     @Test
should_stub_with_answer()75     public void should_stub_with_answer() throws Exception {
76         given(mock.simpleMethod(anyString())).willAnswer(new Answer<String>() {
77             public String answer(InvocationOnMock invocation) throws Throwable {
78                 return invocation.getArgument(0);
79             }
80         });
81 
82         Assertions.assertThat(mock.simpleMethod("foo")).isEqualTo("foo");
83     }
84 
85     @Test
should_stub_with_will_answer_alias()86     public void should_stub_with_will_answer_alias() throws Exception {
87         given(mock.simpleMethod(anyString())).will(new Answer<String>() {
88             public String answer(InvocationOnMock invocation) throws Throwable {
89                 return invocation.getArgument(0);
90             }
91         });
92 
93         Assertions.assertThat(mock.simpleMethod("foo")).isEqualTo("foo");
94     }
95 
96     @Test
should_stub_consecutively()97     public void should_stub_consecutively() throws Exception {
98         given(mock.simpleMethod(anyString()))
99                 .willReturn("foo")
100                 .willReturn("bar");
101 
102         Assertions.assertThat(mock.simpleMethod("whatever")).isEqualTo("foo");
103         Assertions.assertThat(mock.simpleMethod("whatever")).isEqualTo("bar");
104     }
105 
106     @Test
should_return_consecutively()107     public void should_return_consecutively() throws Exception {
108         given(mock.objectReturningMethodNoArgs())
109                 .willReturn("foo", "bar", 12L, new byte[0]);
110 
111         Assertions.assertThat(mock.objectReturningMethodNoArgs()).isEqualTo("foo");
112         Assertions.assertThat(mock.objectReturningMethodNoArgs()).isEqualTo("bar");
113         Assertions.assertThat(mock.objectReturningMethodNoArgs()).isEqualTo(12L);
114         Assertions.assertThat(mock.objectReturningMethodNoArgs()).isEqualTo(new byte[0]);
115     }
116 
117     @Test
should_stub_consecutively_with_call_real_method()118     public void should_stub_consecutively_with_call_real_method() throws Exception {
119         MethodsImpl mock = mock(MethodsImpl.class);
120         willReturn("foo").willCallRealMethod()
121                 .given(mock).simpleMethod();
122 
123         Assertions.assertThat(mock.simpleMethod()).isEqualTo("foo");
124         Assertions.assertThat(mock.simpleMethod()).isEqualTo(null);
125     }
126 
127     @Test
should_stub_void()128     public void should_stub_void() throws Exception {
129         willThrow(new SomethingWasWrong()).given(mock).voidMethod();
130 
131         try {
132             mock.voidMethod();
133             fail();
134         } catch (SomethingWasWrong expected) {
135         }
136     }
137 
138     @Test
should_stub_void_with_exception_class()139     public void should_stub_void_with_exception_class() throws Exception {
140         willThrow(SomethingWasWrong.class).given(mock).voidMethod();
141 
142         try {
143             mock.voidMethod();
144             fail();
145         } catch (SomethingWasWrong expected) {
146         }
147     }
148 
149     @Test
150     @SuppressWarnings("unchecked")
should_stub_void_with_exception_classes()151     public void should_stub_void_with_exception_classes() throws Exception {
152         willThrow(SomethingWasWrong.class, AnotherThingWasWrong.class).given(mock).voidMethod();
153 
154         try {
155             mock.voidMethod();
156             fail();
157         } catch (SomethingWasWrong expected) {
158         }
159     }
160 
161     @Test
should_stub_void_consecutively()162     public void should_stub_void_consecutively() throws Exception {
163         willDoNothing()
164                 .willThrow(new SomethingWasWrong())
165                 .given(mock).voidMethod();
166 
167         mock.voidMethod();
168         try {
169             mock.voidMethod();
170             fail();
171         } catch (SomethingWasWrong expected) {
172         }
173     }
174 
175     @Test
should_stub_void_consecutively_with_exception_class()176     public void should_stub_void_consecutively_with_exception_class() throws Exception {
177         willDoNothing()
178                 .willThrow(SomethingWasWrong.class)
179                 .given(mock).voidMethod();
180 
181         mock.voidMethod();
182         try {
183             mock.voidMethod();
184             fail();
185         } catch (SomethingWasWrong expected) {
186         }
187     }
188 
189     @Test
should_stub_using_do_return_style()190     public void should_stub_using_do_return_style() throws Exception {
191         willReturn("foo").given(mock).simpleMethod("bar");
192 
193         Assertions.assertThat(mock.simpleMethod("boooo")).isEqualTo(null);
194         Assertions.assertThat(mock.simpleMethod("bar")).isEqualTo("foo");
195     }
196 
197     @Test
should_stub_using_do_answer_style()198     public void should_stub_using_do_answer_style() throws Exception {
199         willAnswer(new Answer<String>() {
200             public String answer(InvocationOnMock invocation) throws Throwable {
201                 return invocation.getArgument(0);
202             }
203         })
204                 .given(mock).simpleMethod(anyString());
205 
206         Assertions.assertThat(mock.simpleMethod("foo")).isEqualTo("foo");
207     }
208 
209     @Test
should_stub_by_delegating_to_real_method()210     public void should_stub_by_delegating_to_real_method() throws Exception {
211         //given
212         Dog dog = mock(Dog.class);
213         //when
214         willCallRealMethod().given(dog).bark();
215         //then
216         Assertions.assertThat(dog.bark()).isEqualTo("woof");
217     }
218 
219     @Test
should_stub_by_delegating_to_real_method_using_typical_stubbing_syntax()220     public void should_stub_by_delegating_to_real_method_using_typical_stubbing_syntax() throws Exception {
221         //given
222         Dog dog = mock(Dog.class);
223         //when
224         given(dog.bark()).willCallRealMethod();
225         //then
226         Assertions.assertThat(dog.bark()).isEqualTo("woof");
227     }
228 
229     @Test
should_all_stubbed_mock_reference_access()230     public void should_all_stubbed_mock_reference_access() throws Exception {
231         Set<?> expectedMock = mock(Set.class);
232 
233         Set<?> returnedMock = given(expectedMock.isEmpty()).willReturn(false).getMock();
234 
235         Assertions.assertThat(returnedMock).isEqualTo(expectedMock);
236     }
237 
238     @Test(expected = NotAMockException.class)
should_validate_mock_when_verifying()239     public void should_validate_mock_when_verifying() {
240         then("notMock").should();
241     }
242 
243     @Test(expected = NotAMockException.class)
should_validate_mock_when_verifying_with_expected_number_of_invocations()244     public void should_validate_mock_when_verifying_with_expected_number_of_invocations() {
245         then("notMock").should(times(19));
246     }
247 
248     @Test(expected = NotAMockException.class)
should_validate_mock_when_verifying_no_more_interactions()249     public void should_validate_mock_when_verifying_no_more_interactions() {
250         then("notMock").should();
251     }
252 
253     @Test(expected = WantedButNotInvoked.class)
should_fail_for_expected_behavior_that_did_not_happen()254     public void should_fail_for_expected_behavior_that_did_not_happen() {
255         then(mock).should().booleanObjectReturningMethod();
256     }
257 
258     @Test
should_pass_for_expected_behavior_that_happened()259     public void should_pass_for_expected_behavior_that_happened() {
260         mock.booleanObjectReturningMethod();
261 
262         then(mock).should().booleanObjectReturningMethod();
263         then(mock).shouldHaveNoMoreInteractions();
264     }
265 
266     @Test
should_validate_that_mock_did_not_have_any_interactions()267     public void should_validate_that_mock_did_not_have_any_interactions() {
268         then(mock).shouldHaveZeroInteractions();
269     }
270 
271     @Test
should_fail_when_mock_had_unwanted_interactions()272     public void should_fail_when_mock_had_unwanted_interactions() {
273         mock.booleanObjectReturningMethod();
274 
275         try {
276             then(mock).shouldHaveZeroInteractions();
277             fail("should have reported this interaction wasn't wanted");
278         } catch (NoInteractionsWanted expected) {
279         }
280     }
281 
282     @Test
should_fail_when_mock_had_more_interactions_than_expected()283     public void should_fail_when_mock_had_more_interactions_than_expected() {
284         mock.booleanObjectReturningMethod();
285         mock.byteObjectReturningMethod();
286 
287         then(mock).should().booleanObjectReturningMethod();
288         try {
289             then(mock).shouldHaveNoMoreInteractions();
290             fail("should have reported that no more interactions were wanted");
291         } catch (NoInteractionsWanted expected) {
292         }
293     }
294 
295     @Test
should_pass_for_interactions_that_happened_in_correct_order()296     public void should_pass_for_interactions_that_happened_in_correct_order() {
297         mock.booleanObjectReturningMethod();
298         mock.arrayReturningMethod();
299 
300         InOrder inOrder = inOrder(mock);
301         then(mock).should(inOrder).booleanObjectReturningMethod();
302         then(mock).should(inOrder).arrayReturningMethod();
303     }
304 
305     @Test
should_fail_for_interactions_that_were_in_wrong_order()306     public void should_fail_for_interactions_that_were_in_wrong_order() {
307         InOrder inOrder = inOrder(mock);
308 
309         mock.arrayReturningMethod();
310         mock.booleanObjectReturningMethod();
311 
312         then(mock).should(inOrder).booleanObjectReturningMethod();
313         try {
314             then(mock).should(inOrder).arrayReturningMethod();
315             fail("should have raise in order verification failure on second verify call");
316         } catch (VerificationInOrderFailure expected) {
317         }
318     }
319 
320     @Test(expected = WantedButNotInvoked.class)
should_fail_when_checking_order_of_interactions_that_did_not_happen()321     public void should_fail_when_checking_order_of_interactions_that_did_not_happen() {
322         then(mock).should(inOrder(mock)).booleanObjectReturningMethod();
323     }
324 
325     @Test
should_pass_fluent_bdd_scenario()326     public void should_pass_fluent_bdd_scenario() {
327         Bike bike = new Bike();
328         Person person = mock(Person.class);
329         Police police = mock(Police.class);
330 
331         person.ride(bike);
332         person.ride(bike);
333 
334         then(person).should(times(2)).ride(bike);
335         then(police).shouldHaveZeroInteractions();
336     }
337 
338     @Test
should_pass_fluent_bdd_scenario_with_ordered_verification()339     public void should_pass_fluent_bdd_scenario_with_ordered_verification() {
340         Bike bike = new Bike();
341         Car car = new Car();
342         Person person = mock(Person.class);
343 
344         person.drive(car);
345         person.ride(bike);
346         person.ride(bike);
347 
348         InOrder inOrder = inOrder(person);
349         then(person).should(inOrder).drive(car);
350         then(person).should(inOrder, times(2)).ride(bike);
351     }
352 
353     @Test
should_pass_fluent_bdd_scenario_with_ordered_verification_for_two_mocks()354     public void should_pass_fluent_bdd_scenario_with_ordered_verification_for_two_mocks() {
355         Car car = new Car();
356         Person person = mock(Person.class);
357         Police police = mock(Police.class);
358 
359         person.drive(car);
360         person.drive(car);
361         police.chase(car);
362 
363         InOrder inOrder = inOrder(person, police);
364         then(person).should(inOrder, times(2)).drive(car);
365         then(police).should(inOrder).chase(car);
366     }
367 
368     static class Person {
369 
ride(Bike bike)370         void ride(Bike bike) {
371         }
372 
drive(Car car)373         void drive(Car car) {
374         }
375     }
376 
377     static class Bike {
378 
379     }
380 
381     static class Car {
382 
383     }
384 
385     static class Police {
386 
chase(Car car)387         void chase(Car car) {
388         }
389     }
390 
391     class Dog {
392 
bark()393         public String bark() {
394             return "woof";
395         }
396     }
397 
398     private class SomethingWasWrong extends RuntimeException {
399 
400     }
401 
402     private class AnotherThingWasWrong extends RuntimeException {
403 
404     }
405 }
406