1 package org.mockitousage.junitrunner; 2 3 import org.junit.Before; 4 import org.junit.Rule; 5 import org.junit.Test; 6 import org.junit.runner.JUnitCore; 7 import org.junit.runner.Result; 8 import org.junit.runner.RunWith; 9 import org.mockito.Mock; 10 import org.mockito.exceptions.misusing.UnnecessaryStubbingException; 11 import org.mockito.junit.MockitoJUnit; 12 import org.mockito.junit.MockitoRule; 13 import org.mockito.junit.MockitoJUnitRunner; 14 import org.mockitousage.IMethods; 15 import org.mockitoutil.JUnitResultAssert; 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 22 23 public class StrictRunnerTest extends TestBase { 24 25 JUnitCore runner = new JUnitCore(); 26 27 @Test public void succeeds_when_all_stubs_were_used() { 28 //when 29 Result result = runner.run( 30 StubbingInConstructorUsed.class, 31 StubbingInBeforeUsed.class, 32 StubbingInTestUsed.class 33 ); 34 //then 35 JUnitResultAssert.assertThat(result).isSuccessful(); 36 } 37 38 @Test public void fails_when_stubs_were_not_used() { 39 Class[] tests = {StubbingInConstructorUnused.class, 40 StubbingInBeforeUnused.class, 41 StubbingInTestUnused.class}; 42 43 //when 44 Result result = runner.run(tests); 45 46 //then 47 JUnitResultAssert.assertThat(result).fails(3, UnnecessaryStubbingException.class); 48 } 49 50 @Test public void does_not_report_unused_stubs_when_different_failure_is_present() { 51 //when 52 Result result = runner.run(WithUnrelatedAssertionFailure.class); 53 54 //then 55 JUnitResultAssert.assertThat(result).fails(1, MyAssertionError.class); 56 } 57 58 @Test public void runner_can_coexist_with_rule() { 59 //I don't believe that this scenario is useful 60 //I only wish that Mockito does not break awkwardly when both: runner & rule is used 61 62 //when 63 Result result = runner.run(RunnerAndRule.class); 64 65 //then 66 JUnitResultAssert.assertThat(result).fails(1, UnnecessaryStubbingException.class); 67 } 68 69 @Test public void runner_in_multi_threaded_tests() { 70 //when 71 Result result = runner.run(StubUsedFromDifferentThread.class); 72 73 //then 74 JUnitResultAssert.assertThat(result).isSuccessful(); 75 } 76 77 @RunWith(MockitoJUnitRunner.class) 78 public static class StubbingInConstructorUsed extends StubbingInConstructorUnused { 79 @Test public void test() { 80 assertEquals("1", mock.simpleMethod(1)); 81 } 82 } 83 84 @RunWith(MockitoJUnitRunner.Strict.class) //using Strict to make sure it does the right thing 85 public static class StubbingInConstructorUnused { 86 IMethods mock = when(mock(IMethods.class).simpleMethod(1)).thenReturn("1").getMock(); 87 @Test public void dummy() {} 88 } 89 90 @RunWith(MockitoJUnitRunner.class) 91 public static class StubbingInBeforeUsed extends StubbingInBeforeUnused { 92 @Test public void test() { 93 assertEquals("1", mock.simpleMethod(1)); 94 } 95 } 96 97 @RunWith(MockitoJUnitRunner.class) 98 public static class StubbingInBeforeUnused { 99 @Mock IMethods mock; 100 @Before public void before() { 101 when(mock.simpleMethod(1)).thenReturn("1"); 102 } 103 @Test public void dummy() {} 104 } 105 106 @RunWith(MockitoJUnitRunner.class) 107 public static class StubbingInTestUsed { 108 @Test public void test() { 109 IMethods mock = mock(IMethods.class); 110 when(mock.simpleMethod(1)).thenReturn("1"); 111 assertEquals("1", mock.simpleMethod(1)); 112 } 113 } 114 115 @RunWith(MockitoJUnitRunner.class) 116 public static class StubbingInTestUnused { 117 @Test public void test() { 118 IMethods mock = mock(IMethods.class); 119 when(mock.simpleMethod(1)).thenReturn("1"); 120 mock.simpleMethod(2); //different arg 121 } 122 } 123 124 private static class MyAssertionError extends AssertionError {} 125 126 @RunWith(MockitoJUnitRunner.class) 127 public static class WithUnrelatedAssertionFailure { 128 129 IMethods mock = mock(IMethods.class); 130 IMethods mock2 = mock(IMethods.class); 131 132 @Before public void before() { 133 when(mock2.simpleMethod("unused stubbing")).thenReturn(""); 134 } 135 136 @Test public void passing_test() { 137 when(mock.simpleMethod(1)).thenReturn("1"); 138 assertEquals("1", mock.simpleMethod(1)); 139 } 140 141 @Test public void failing_test() { 142 throw new MyAssertionError(); 143 } 144 } 145 146 @RunWith(MockitoJUnitRunner.class) 147 public static class RunnerAndRule { 148 149 public @Rule MockitoRule rule = MockitoJUnit.rule(); 150 IMethods mock = mock(IMethods.class); 151 152 @Test public void passing_test() { 153 when(mock.simpleMethod(1)).thenReturn("1"); 154 mock.simpleMethod(2); 155 } 156 } 157 158 @RunWith(MockitoJUnitRunner.class) 159 public static class StubUsedFromDifferentThread { 160 161 IMethods mock = mock(IMethods.class); 162 163 @Test public void passing_test() throws Exception { 164 //stubbing is done in main thread: 165 when(mock.simpleMethod(1)).thenReturn("1"); 166 167 //stubbing is used in a different thread 168 //stubbing should not be reported as unused by the runner 169 Thread t = new Thread() { 170 public void run() { 171 mock.simpleMethod(1); 172 } 173 }; 174 t.start(); 175 t.join(); 176 } 177 } 178 } 179