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.junitrule; 6 7 import org.junit.Rule; 8 import org.junit.Test; 9 import org.mockito.Mock; 10 import org.mockito.quality.Strictness; 11 import org.mockito.internal.junit.JUnitRule; 12 import org.mockito.internal.util.SimpleMockitoLogger; 13 import org.mockitousage.IMethods; 14 import org.mockitoutil.SafeJUnitRule; 15 16 import static org.junit.Assert.assertEquals; 17 import static org.junit.Assert.assertTrue; 18 import static org.mockito.Mockito.when; 19 import static org.mockitoutil.TestBase.filterLineNo; 20 21 public class StubbingWarningsJUnitRuleTest { 22 23 private SimpleMockitoLogger logger = new SimpleMockitoLogger(); 24 @Rule public SafeJUnitRule rule = new SafeJUnitRule(new JUnitRule(logger, Strictness.WARN)); 25 @Mock IMethods mock; 26 27 @Test no_unused_stubs_reported_on_failure()28 public void no_unused_stubs_reported_on_failure() throws Throwable { 29 //expect 30 rule.expectFailure(new SafeJUnitRule.FailureAssert() { 31 public void doAssert(Throwable t) { 32 assertEquals("x", t.getMessage()); 33 assertTrue(logger.getLoggedInfo().isEmpty()); 34 } 35 }); 36 37 //when 38 declareStubbing(mock); 39 throw new AssertionError("x"); 40 } 41 42 @Test stubbing_arg_mismatch_on_failure()43 public void stubbing_arg_mismatch_on_failure() throws Throwable { 44 //expect 45 rule.expectFailure(new SafeJUnitRule.FailureAssert() { 46 public void doAssert(Throwable t) { 47 assertEquals("x", t.getMessage()); 48 assertEquals( 49 "[MockitoHint] StubbingWarningsJUnitRuleTest.stubbing_arg_mismatch_on_failure (see javadoc for MockitoHint):\n" + 50 "[MockitoHint] 1. Unused... -> at org.mockitousage.junitrule.StubbingWarningsJUnitRuleTest.declareStubbingWithArg(StubbingWarningsJUnitRuleTest.java:0)\n" + 51 "[MockitoHint] ...args ok? -> at org.mockitousage.junitrule.StubbingWarningsJUnitRuleTest.useStubbingWithArg(StubbingWarningsJUnitRuleTest.java:0)\n", 52 filterLineNo(logger.getLoggedInfo())); 53 } 54 }); 55 56 //when 57 declareStubbingWithArg(mock, "a"); 58 useStubbingWithArg(mock, "b"); 59 throw new AssertionError("x"); 60 } 61 no_stubbing_arg_mismatch_when_no_mismatch_on_fail()62 @Test public void no_stubbing_arg_mismatch_when_no_mismatch_on_fail() throws Throwable { 63 //expect 64 rule.expectFailure(new SafeJUnitRule.FailureAssert() { 65 public void doAssert(Throwable t) { 66 assertEquals("x", t.getMessage()); 67 assertTrue(logger.getLoggedInfo().isEmpty()); 68 } 69 }); 70 71 //when 72 declareStubbingWithArg(mock, "a"); 73 useStubbingWithArg(mock, "a"); 74 throw new AssertionError("x"); 75 } 76 77 @Test no_stubbing_warning_on_pass()78 public void no_stubbing_warning_on_pass() throws Throwable { 79 //expect 80 rule.expectSuccess(new Runnable() { 81 public void run() { 82 assertTrue(logger.isEmpty()); 83 } 84 }); 85 86 //when 87 declareStubbingWithArg(mock, "a"); 88 useStubbingWithArg(mock, "a"); 89 } 90 91 @Test multiple_stubbing_arg_mismatch_on_failure()92 public void multiple_stubbing_arg_mismatch_on_failure() throws Throwable { 93 //expect 94 rule.expectFailure(new SafeJUnitRule.FailureAssert() { 95 public void doAssert(Throwable t) { 96 assertEquals("x", t.getMessage()); 97 assertEquals( 98 "[MockitoHint] StubbingWarningsJUnitRuleTest.multiple_stubbing_arg_mismatch_on_failure (see javadoc for MockitoHint):\n" + 99 "[MockitoHint] 1. Unused... -> at org.mockitousage.junitrule.StubbingWarningsJUnitRuleTest.declareStubbingWithArg(StubbingWarningsJUnitRuleTest.java:0)\n" + 100 "[MockitoHint] ...args ok? -> at org.mockitousage.junitrule.StubbingWarningsJUnitRuleTest.useStubbingWithArg(StubbingWarningsJUnitRuleTest.java:0)\n" + 101 "[MockitoHint] ...args ok? -> at org.mockitousage.junitrule.StubbingWarningsJUnitRuleTest.useStubbingWithArg(StubbingWarningsJUnitRuleTest.java:0)\n" + 102 "[MockitoHint] 2. Unused... -> at org.mockitousage.junitrule.StubbingWarningsJUnitRuleTest.declareStubbingWithArg(StubbingWarningsJUnitRuleTest.java:0)\n" + 103 "[MockitoHint] ...args ok? -> at org.mockitousage.junitrule.StubbingWarningsJUnitRuleTest.useStubbingWithArg(StubbingWarningsJUnitRuleTest.java:0)\n" + 104 "[MockitoHint] ...args ok? -> at org.mockitousage.junitrule.StubbingWarningsJUnitRuleTest.useStubbingWithArg(StubbingWarningsJUnitRuleTest.java:0)\n", 105 filterLineNo(logger.getLoggedInfo())); 106 } 107 }); 108 109 //when 110 declareStubbingWithArg(mock, "a"); 111 declareStubbingWithArg(mock, "b"); 112 113 useStubbingWithArg(mock, "c"); 114 useStubbingWithArg(mock, "d"); 115 116 throw new AssertionError("x"); 117 } 118 119 @Test reports_only_mismatching_stubs()120 public void reports_only_mismatching_stubs() throws Throwable { 121 //expect 122 rule.expectFailure(new SafeJUnitRule.FailureAssert() { 123 public void doAssert(Throwable t) { 124 assertEquals("x", t.getMessage()); 125 assertEquals( 126 "[MockitoHint] StubbingWarningsJUnitRuleTest.reports_only_mismatching_stubs (see javadoc for MockitoHint):\n" + 127 "[MockitoHint] 1. Unused... -> at org.mockitousage.junitrule.StubbingWarningsJUnitRuleTest.declareStubbingWithArg(StubbingWarningsJUnitRuleTest.java:0)\n" + 128 "[MockitoHint] ...args ok? -> at org.mockitousage.junitrule.StubbingWarningsJUnitRuleTest.useStubbingWithArg(StubbingWarningsJUnitRuleTest.java:0)\n", 129 filterLineNo(logger.getLoggedInfo())); 130 } 131 }); 132 133 //when 134 declareStubbingWithArg(mock, "a"); // <-- used 135 declareStubbingWithArg(mock, "b"); // <-- unused 136 137 useStubbingWithArg(mock, "a"); 138 useStubbingWithArg(mock, "d"); // <-- arg mismatch 139 140 throw new AssertionError("x"); 141 } 142 143 @Test no_mismatch_when_stub_was_used()144 public void no_mismatch_when_stub_was_used() throws Throwable { 145 //expect 146 rule.expectFailure(new SafeJUnitRule.FailureAssert() { 147 public void doAssert(Throwable t) { 148 assertEquals("x", t.getMessage()); 149 assertTrue(logger.getLoggedInfo().isEmpty()); 150 } 151 }); 152 153 //when 154 declareStubbingWithArg(mock, "a"); 155 156 useStubbingWithArg(mock, "a"); 157 useStubbingWithArg(mock, "d"); // <-- arg mismatch, but the stub was already used 158 159 throw new AssertionError("x"); 160 } 161 162 @Test no_stubbing_arg_mismatch_on_pass()163 public void no_stubbing_arg_mismatch_on_pass() throws Throwable { 164 //expect 165 rule.expectSuccess(new Runnable() { 166 public void run() { 167 assertEquals( 168 "[MockitoHint] StubbingWarningsJUnitRuleTest.no_stubbing_arg_mismatch_on_pass (see javadoc for MockitoHint):\n" + 169 "[MockitoHint] 1. Unused -> at org.mockitousage.junitrule.StubbingWarningsJUnitRuleTest.declareStubbingWithArg(StubbingWarningsJUnitRuleTest.java:0)\n", 170 filterLineNo(logger.getLoggedInfo())); 171 } 172 }); 173 174 //when 175 declareStubbingWithArg(mock, "a"); 176 useStubbingWithArg(mock, "b"); 177 } 178 179 @Test warns_about_unused_stubs_when_passed()180 public void warns_about_unused_stubs_when_passed() throws Throwable { 181 //expect 182 rule.expectSuccess(new Runnable() { 183 public void run() { 184 assertEquals( 185 "[MockitoHint] StubbingWarningsJUnitRuleTest.warns_about_unused_stubs_when_passed (see javadoc for MockitoHint):\n" + 186 "[MockitoHint] 1. Unused -> at org.mockitousage.junitrule.StubbingWarningsJUnitRuleTest.declareStubbing(StubbingWarningsJUnitRuleTest.java:0)\n", 187 filterLineNo(logger.getLoggedInfo())); 188 189 } 190 }); 191 192 //when 193 declareStubbing(mock); 194 } 195 declareStubbingWithArg(IMethods mock, String arg)196 private static void declareStubbingWithArg(IMethods mock, String arg) { 197 when(mock.simpleMethod(arg)).thenReturn("bar"); 198 } 199 declareStubbing(IMethods mock)200 private static void declareStubbing(IMethods mock) { 201 when(mock.simpleMethod("foo")).thenReturn("bar"); 202 } 203 useStubbingWithArg(IMethods mock, String arg)204 private void useStubbingWithArg(IMethods mock, String arg) { 205 mock.simpleMethod(arg); 206 } 207 } 208