• 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.junitrunner;
6 
7 import org.junit.Test;
8 import org.junit.runner.JUnitCore;
9 import org.junit.runner.Result;
10 import org.junit.runner.RunWith;
11 import org.mockito.Mock;
12 import org.mockito.exceptions.misusing.UnfinishedStubbingException;
13 import org.mockito.internal.util.SimpleMockitoLogger;
14 import org.mockito.junit.TestableJUnitRunner;
15 import org.mockitousage.IMethods;
16 import org.mockitoutil.TestBase;
17 
18 import static org.junit.Assert.assertEquals;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.when;
21 import static org.mockitoutil.JUnitResultAssert.assertThat;
22 
23 public class StubbingWarningsJUnitRunnerTest extends TestBase {
24 
25     JUnitCore runner = new JUnitCore();
26     SimpleMockitoLogger logger = TestableJUnitRunner.refreshedLogger();
27 
no_arg_mismatch_warnings()28     @Test public void no_arg_mismatch_warnings() {
29         //when
30         runner.run(PassingArgMismatch.class, FailingWithMatchingArgs.class, MismatchButStubAlreadyUsed.class);
31 
32         //then
33         assertEquals("", filterLineNo(logger.getLoggedInfo()));
34     }
35 
shows_arg_mismatch_warnings_when_test_fails()36     @Test public void shows_arg_mismatch_warnings_when_test_fails() {
37         //when
38         runner.run(FailingWithArgMismatch.class);
39 
40         //then
41         assertEquals("[MockitoHint] FailingWithArgMismatch.test (see javadoc for MockitoHint):\n" +
42                 "[MockitoHint] 1. Unused... -> at org.mockitousage.junitrunner.StubbingWarningsJUnitRunnerTest$FailingWithArgMismatch.test(StubbingWarningsJUnitRunnerTest.java:0)\n" +
43                 "[MockitoHint]  ...args ok? -> at org.mockitousage.junitrunner.StubbingWarningsJUnitRunnerTest$FailingWithArgMismatch.test(StubbingWarningsJUnitRunnerTest.java:0)\n", filterLineNo(logger.getLoggedInfo()));
44     }
45 
shows_arg_mismatch_warnings_only_for_mismatches()46     @Test public void shows_arg_mismatch_warnings_only_for_mismatches() {
47         //when
48         runner.run(FailingWithSomeStubMismatches.class);
49 
50         //then
51         assertEquals("[MockitoHint] FailingWithSomeStubMismatches.test (see javadoc for MockitoHint):\n" +
52                 "[MockitoHint] 1. Unused... -> at org.mockitousage.junitrunner.StubbingWarningsJUnitRunnerTest$FailingWithSomeStubMismatches.test(StubbingWarningsJUnitRunnerTest.java:0)\n" +
53                 "[MockitoHint]  ...args ok? -> at org.mockitousage.junitrunner.StubbingWarningsJUnitRunnerTest$FailingWithSomeStubMismatches.test(StubbingWarningsJUnitRunnerTest.java:0)\n", filterLineNo(logger.getLoggedInfo()));
54     }
55 
validates_mockito_usage()56     @Test public void validates_mockito_usage() {
57         //when
58         Result result = runner.run(InvalidMockitoUsage.class);
59 
60         //then
61         assertThat(result).fails(1, UnfinishedStubbingException.class);
62     }
63 
64     @RunWith(TestableJUnitRunner.class)
65     public static class PassingArgMismatch {
66         IMethods mock = mock(IMethods.class);
test()67         @Test public void test() throws Exception {
68             when(mock.simpleMethod(1)).thenReturn("1");
69             mock.simpleMethod(2);
70         }
71     }
72 
73     @RunWith(TestableJUnitRunner.class)
74     public static class FailingWithArgMismatch {
75         @Mock IMethods mock;
test()76         @Test public void test() throws Exception {
77             when(mock.simpleMethod(1)).thenReturn("1");
78             mock.simpleMethod(2);
79             throw new RuntimeException("x");
80         }
81     }
82 
83     @RunWith(TestableJUnitRunner.class)
84     public static class FailingWithMatchingArgs {
85         @Mock IMethods mock;
test()86         @Test public void test() throws Exception {
87             when(mock.simpleMethod(1)).thenReturn("1");
88             mock.simpleMethod(1);
89             throw new RuntimeException("x");
90         }
91     }
92 
93     @RunWith(TestableJUnitRunner.class)
94     public static class FailingWithSomeStubMismatches {
95         @Mock IMethods mock;
test()96         @Test public void test() throws Exception {
97             when(mock.simpleMethod(1)).thenReturn("1"); // <- used
98             when(mock.simpleMethod(2)).thenReturn("2"); // <- unused
99 
100             mock.simpleMethod(1); // <- not reported
101             mock.simpleMethod(3); // <- reported
102 
103             throw new RuntimeException("x");
104         }
105     }
106 
107     @RunWith(TestableJUnitRunner.class)
108     public static class MismatchButStubAlreadyUsed {
109         @Mock IMethods mock;
test()110         @Test public void test() throws Exception {
111             when(mock.simpleMethod(1)).thenReturn("1");
112             mock.simpleMethod(1); // <-- used
113             mock.simpleMethod(2); // <-- arg mismatch, but the stub was already used
114 
115             throw new RuntimeException("x");
116         }
117     }
118 
119     @RunWith(TestableJUnitRunner.class)
120     public static class InvalidMockitoUsage {
121         @Mock IMethods mock;
122         @SuppressWarnings({"MockitoUsage", "CheckReturnValue"})
test()123         @Test public void test() throws Exception {
124             when(mock.simpleMethod()); // <-- unfinished stubbing
125         }
126     }
127 }
128