• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockitousage.session;
6 
7 import org.junit.After;
8 import org.junit.Test;
9 import org.junit.runner.JUnitCore;
10 import org.junit.runner.Result;
11 import org.mockito.Mock;
12 import org.mockito.Mockito;
13 import org.mockito.MockitoSession;
14 import org.mockito.exceptions.misusing.UnfinishedStubbingException;
15 import org.mockito.quality.Strictness;
16 import org.mockitousage.IMethods;
17 import org.mockitoutil.JUnitResultAssert;
18 import org.mockitoutil.TestBase;
19 
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.Mockito.mockingDetails;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 public class MockitoSessionTest extends TestBase {
29 
30     private JUnitCore junit = new JUnitCore();
31 
session_without_any_configuration()32     @Test public void session_without_any_configuration() {
33         //when
34         Result result = junit.run(MockitoSessionTest.SessionWithoutAnyConfiguration.class);
35 
36         //expect
37         JUnitResultAssert.assertThat(result).succeeds(1);
38     }
39 
session_without_init_mocks_configured()40     @Test public void session_without_init_mocks_configured() {
41         //when
42         Result result = junit.run(MockitoSessionTest.SessionWithoutInitMocksConfigured.class);
43 
44         //expect
45         JUnitResultAssert.assertThat(result).succeeds(1);
46     }
47 
session_without_strictness_configured()48     @Test public void session_without_strictness_configured() {
49         //when
50         Result result = junit.run(MockitoSessionTest.SessionWithoutStrictnessConfigured.class);
51 
52         //expect
53         JUnitResultAssert.assertThat(result).succeeds(1);
54     }
55 
session_with_incorrect_mockito_usage()56     @Test public void session_with_incorrect_mockito_usage() {
57         //when
58         Result result = junit.run(MockitoSessionTest.SessionWithIncorrectMockitoUsage.class);
59 
60         //expect
61         JUnitResultAssert.assertThat(result).fails(1, UnfinishedStubbingException.class);
62     }
63 
reports_other_failure_and_incorrect_mockito_usage()64     @Test public void reports_other_failure_and_incorrect_mockito_usage() {
65         //when
66         Result result = junit.run(MockitoSessionTest.SessionWithTestFailureAndIncorrectMockitoUsage.class);
67 
68         //expect
69         JUnitResultAssert.assertThat(result)
70                 .failsExactly(AssertionError.class, UnfinishedStubbingException.class);
71     }
72 
allows_initializing_mocks_manually()73     @Test public void allows_initializing_mocks_manually() {
74         //when
75         Result result = junit.run(MockitoSessionTest.SessionWithManuallyInitializedMock.class);
76 
77         //expect
78         JUnitResultAssert.assertThat(result).succeeds(1);
79     }
80 
allows_updating_strictness()81     @Test public void allows_updating_strictness() {
82         //when
83         Result result = junit.run(MockitoSessionTest.SessionWithUpdatedStrictness.class);
84 
85         //expect
86         JUnitResultAssert.assertThat(result).succeeds(1);
87     }
88 
allows_overriding_failure()89     @Test public void allows_overriding_failure() {
90         //when
91         Result result = junit.run(MockitoSessionTest.SessionWithOverriddenFailure.class);
92 
93         //expect
94         JUnitResultAssert.assertThat(result).isSuccessful();
95 
96         //in order to demonstrate feature, we intentionally misuse Mockito and need to clean up state
97         resetState();
98     }
99 
100     public static class SessionWithoutAnyConfiguration {
101 
102         @Mock IMethods mock;
103 
104         //session without initMocks is not currently supported
105         MockitoSession mockito = Mockito.mockitoSession().startMocking();
106 
after()107         @After public void after() {
108             mockito.finishMocking();
109         }
110 
some_test()111         @Test public void some_test() {
112             assertNull(mock); //initMocks() was not used when configuring session
113         }
114     }
115 
116     public static class SessionWithoutInitMocksConfigured {
117 
118         @Mock IMethods mock;
119 
120         MockitoSession mockito = Mockito.mockitoSession().strictness(Strictness.LENIENT).startMocking();
121 
after()122         @After public void after() {
123             mockito.finishMocking();
124         }
125 
some_test()126         @Test public void some_test() {
127             assertNull(mock); //initMocks() was not used when configuring session
128         }
129     }
130 
131     public static class SessionWithoutStrictnessConfigured {
132         @Mock IMethods mock;
133 
134         MockitoSession mockito = Mockito.mockitoSession().initMocks(this).startMocking();
135 
after()136         @After public void after() {
137             mockito.finishMocking();
138         }
139 
some_test()140         @Test public void some_test() {
141             assertNotNull(mock);
142         }
143     }
144 
145     public static class SessionWithIncorrectMockitoUsage {
146         @Mock IMethods mock;
147 
148         MockitoSession mockito = Mockito.mockitoSession().initMocks(this).startMocking();
149 
after()150         @After public void after() {
151             mockito.finishMocking();
152         }
153 
154         @SuppressWarnings({"MockitoUsage", "CheckReturnValue"})
unfinished_stubbing()155         @Test public void unfinished_stubbing() {
156             when(mock.simpleMethod());
157         }
158     }
159 
160     public static class SessionWithTestFailureAndIncorrectMockitoUsage {
161         @Mock IMethods mock;
162 
163         MockitoSession mockito = Mockito.mockitoSession().initMocks(this).startMocking();
164 
after()165         @After public void after() {
166             mockito.finishMocking();
167         }
168 
169         @SuppressWarnings({"MockitoUsage", "CheckReturnValue"})
unfinished_stubbing_with_other_failure()170         @Test public void unfinished_stubbing_with_other_failure() {
171             when(mock.simpleMethod());
172             assertTrue(false);
173         }
174     }
175 
176     public static class SessionWithManuallyInitializedMock {
177         @Mock IMethods mock;
178         IMethods mock2 = Mockito.mock(IMethods.class, "manual mock");
179 
180         MockitoSession mockito = Mockito.mockitoSession().initMocks(this).startMocking();
181 
after()182         @After public void after() {
183             mockito.finishMocking();
184         }
185 
manual_mock_preserves_its_settings()186         @Test public void manual_mock_preserves_its_settings() {
187             assertEquals("mock", mockingDetails(mock).getMockCreationSettings().getMockName().toString());
188             assertEquals("manual mock", mockingDetails(mock2).getMockCreationSettings().getMockName().toString());
189         }
190     }
191 
192     public static class SessionWithUpdatedStrictness {
193         @Mock IMethods mock;
194         MockitoSession mockito = Mockito.mockitoSession().initMocks(this).strictness(Strictness.STRICT_STUBS).startMocking();
195 
after()196         @After public void after() {
197             mockito.finishMocking();
198         }
199 
manual_mock_preserves_its_settings()200         @Test public void manual_mock_preserves_its_settings() {
201             when(mock.simpleMethod(1)).thenReturn("foo");
202 
203             //when
204             mockito.setStrictness(Strictness.LENIENT);
205 
206             //then no exception is thrown, even though the arg is different
207             mock.simpleMethod(2);
208         }
209     }
210 
211     public static class SessionWithOverriddenFailure {
212         @Mock IMethods mock;
213         MockitoSession mockito = Mockito.mockitoSession().initMocks(this).startMocking();
214 
after()215         @After public void after() {
216             mockito.finishMocking(new RuntimeException("Boo!"));
217         }
218 
219         @SuppressWarnings({"MockitoUsage", "CheckReturnValue"})
invalid_mockito_usage()220         @Test public void invalid_mockito_usage() {
221             verify(mock);
222         }
223     }
224 }
225