• 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.stubbing;
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.StateMaster;
15 import org.mockito.exceptions.misusing.PotentialStubbingProblem;
16 import org.mockito.exceptions.misusing.UnfinishedMockingSessionException;
17 import org.mockito.exceptions.misusing.UnnecessaryStubbingException;
18 import org.mockito.quality.Strictness;
19 import org.mockitousage.IMethods;
20 
21 import java.util.Map;
22 import java.util.concurrent.ConcurrentHashMap;
23 
24 import static org.mockito.BDDMockito.given;
25 import static org.mockitoutil.ConcurrentTesting.concurrently;
26 import static org.mockitoutil.JUnitResultAssert.assertThat;
27 
28 public class StrictStubbingEndToEndTest {
29 
30     JUnitCore junit = new JUnitCore();
31 
after()32     @After public void after() {
33         new StateMaster().clearMockitoListeners();
34     }
35 
finish_mocking_exception_does_not_hide_the_exception_from_test()36     @Test public void finish_mocking_exception_does_not_hide_the_exception_from_test() {
37         Result result = junit.run(UnnecessaryStubbing.class);
38         assertThat(result)
39                 //both exceptions are reported to JUnit:
40                 .fails("unnecessary_stubbing", IllegalStateException.class)
41                 .fails("unnecessary_stubbing", UnnecessaryStubbingException.class);
42     }
43 
does_not_report_unused_stubbing_if_mismatch_reported()44     @Test public void does_not_report_unused_stubbing_if_mismatch_reported() {
45         Result result = junit.run(ReportMismatchButNotUnusedStubbing.class);
46         assertThat(result).fails(1, PotentialStubbingProblem.class);
47     }
48 
strict_stubbing_does_not_leak_to_other_tests()49     @Test public void strict_stubbing_does_not_leak_to_other_tests() {
50         Result result = junit.run(LenientStrictness1.class, StrictStubsPassing.class, LenientStrictness2.class);
51         //all tests pass, lenient test cases contain incorrect stubbing
52         assertThat(result).succeeds(5);
53     }
54 
detects_unfinished_session()55     @Test public void detects_unfinished_session() {
56         Result result = junit.run(UnfinishedMocking.class);
57         assertThat(result)
58             .fails(UnfinishedMockingSessionException.class, "\n" +
59                 "Unfinished mocking session detected.\n" +
60                 "Previous MockitoSession was not concluded with 'finishMocking()'.\n" +
61                 "For examples of correct usage see javadoc for MockitoSession class.");
62     }
63 
concurrent_sessions_in_different_threads()64     @Test public void concurrent_sessions_in_different_threads() throws Exception {
65         final Map<Class, Result> results = new ConcurrentHashMap<Class, Result>();
66         concurrently(new Runnable() {
67                          public void run() {
68                              results.put(StrictStubsPassing.class, junit.run(StrictStubsPassing.class));
69                          }
70                      }, new Runnable() {
71                          public void run() {
72                              results.put(ReportMismatchButNotUnusedStubbing.class, junit.run(ReportMismatchButNotUnusedStubbing.class));
73                          }
74                      }
75         );
76 
77         assertThat(results.get(StrictStubsPassing.class)).succeeds(1);
78         assertThat(results.get(ReportMismatchButNotUnusedStubbing.class)).fails(1);
79     }
80 
81     public static class UnnecessaryStubbing {
82         @Mock IMethods mock;
83         MockitoSession mockito = Mockito.mockitoSession().initMocks(this).strictness(Strictness.STRICT_STUBS).startMocking();
84 
after()85         @After public void after() {
86             mockito.finishMocking();
87         }
88 
unnecessary_stubbing()89         @Test public void unnecessary_stubbing() {
90             given(mock.simpleMethod("1")).willReturn("one");
91             throw new IllegalStateException();
92         }
93     }
94 
95     public static class ReportMismatchButNotUnusedStubbing {
96         @Mock IMethods mock;
97         MockitoSession mockito = Mockito.mockitoSession().initMocks(this).strictness(Strictness.STRICT_STUBS).startMocking();
98 
after()99         @After public void after() {
100             mockito.finishMocking();
101         }
102 
mismatch()103         @Test public void mismatch() {
104             given(mock.simpleMethod(1)).willReturn("");
105             mock.simpleMethod(2);
106         }
107     }
108 
109     public static class StrictStubsPassing {
110         @Mock IMethods mock;
111         MockitoSession mockito = Mockito.mockitoSession().initMocks(this).strictness(Strictness.STRICT_STUBS).startMocking();
112 
after()113         @After public void after() {
114             mockito.finishMocking();
115         }
116 
used()117         @Test public void used() {
118             given(mock.simpleMethod(1)).willReturn("");
119             mock.simpleMethod(1);
120         }
121     }
122 
123     public static class LenientStrictness1 {
124         @Mock IMethods mock = Mockito.mock(IMethods.class);
125 
unused()126         @Test public void unused() {
127             given(mock.simpleMethod(1)).willReturn("");
128         }
129 
mismatch()130         @Test public void mismatch() {
131             given(mock.simpleMethod(2)).willReturn("");
132             mock.simpleMethod(3);
133         }
134     }
135 
136     public static class LenientStrictness2 {
137         @Mock IMethods mock = Mockito.mock(IMethods.class);
138 
unused()139         @Test public void unused() {
140             given(mock.simpleMethod(1)).willReturn("");
141         }
142 
mismatch()143         @Test public void mismatch() {
144             given(mock.simpleMethod(2)).willReturn("");
145             mock.simpleMethod(3);
146         }
147     }
148 
149     public static class UnfinishedMocking {
150         @Mock IMethods mock;
151         MockitoSession mockito = Mockito.mockitoSession().initMocks(this).strictness(Strictness.STRICT_STUBS).startMocking();
152 
unused()153         @Test public void unused() {
154             given(mock.simpleMethod("1")).willReturn("one");
155         }
156 
unused2()157         @Test public void unused2() {
158             given(mock.simpleMethod("1")).willReturn("one");
159         }
160     }
161 }
162