• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.mockitousage.stubbing;
2 
3 import org.junit.Test;
4 import org.mockito.Mock;
5 import org.mockito.MockitoSession;
6 import org.mockito.internal.framework.DefaultMockitoSession;
7 import org.mockito.internal.util.SimpleMockitoLogger;
8 import org.mockito.quality.Strictness;
9 import org.mockitousage.IMethods;
10 
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.BDDMockito.given;
13 import static org.mockitoutil.TestBase.filterLineNo;
14 
15 public class StubbingWarningsTest {
16 
17     @Mock IMethods mock;
18 
19     SimpleMockitoLogger logger = new SimpleMockitoLogger();
20     MockitoSession mockito = new DefaultMockitoSession(this, Strictness.WARN, logger);
21 
few_interactions()22     @Test public void few_interactions() throws Throwable {
23         //when
24         mock.simpleMethod(100);
25         mock.otherMethod();
26 
27         //expect no exception
28         mockito.finishMocking();
29         logger.assertEmpty();
30     }
31 
stubbing_used()32     @Test public void stubbing_used() throws Throwable {
33         //when
34         given(mock.simpleMethod(100)).willReturn("100");
35         mock.simpleMethod(100);
36 
37         //then
38         mockito.finishMocking();
39         logger.assertEmpty();
40     }
41 
unused_stubbed_is_not_implicitly_verified()42     @Test public void unused_stubbed_is_not_implicitly_verified() throws Throwable {
43         //when
44         given(mock.simpleMethod(100)).willReturn("100");
45         mock.simpleMethod(100); // <- stubbing is used
46         mock.simpleMethod(200); // <- other method should not generate arg mismatch
47 
48         //then
49         mockito.finishMocking();
50         logger.assertEmpty();
51     }
52 
stubbing_argument_mismatch()53     @Test public void stubbing_argument_mismatch() throws Throwable {
54         //when
55         given(mock.simpleMethod(100)).willReturn("100");
56         mock.simpleMethod(200);
57 
58         mockito.finishMocking();
59 
60         //TODO - currently we warn about "Unused" instead of "Arg mismatch" below
61         //because it was simpler to implement. This can be improved given we put priority to improve the warnings.
62         //then
63         assertEquals(filterLineNo(
64             "[MockitoHint] StubbingWarningsTest.null (see javadoc for MockitoHint):\n" +
65             "[MockitoHint] 1. Unused -> at org.mockitousage.stubbing.StubbingWarningsTest.stubbing_argument_mismatch(StubbingWarningsTest.java:0)\n"),
66                 filterLineNo(logger.getLoggedInfo()));
67     }
68 
unused_stubbing()69     @Test public void unused_stubbing() throws Throwable {
70         //when
71         given(mock.simpleMethod(100)).willReturn("100");
72 
73         mockito.finishMocking();
74 
75         //then
76         assertEquals(filterLineNo(
77             "[MockitoHint] StubbingWarningsTest.null (see javadoc for MockitoHint):\n" +
78             "[MockitoHint] 1. Unused -> at org.mockitousage.stubbing.StubbingWarningsTest.unused_stubbing(StubbingWarningsTest.java:0)\n"),
79                 filterLineNo(logger.getLoggedInfo()));
80     }
81 }
82