1 package org.mockitousage.junitrunner; 2 3 import java.util.List; 4 import org.junit.Test; 5 import org.junit.runner.JUnitCore; 6 import org.junit.runner.Result; 7 import org.junit.runner.RunWith; 8 import org.mockito.Mock; 9 import org.mockito.exceptions.misusing.UnfinishedStubbingException; 10 import org.mockito.exceptions.verification.TooLittleActualInvocations; 11 import org.mockito.junit.MockitoJUnitRunner; 12 import org.mockitousage.IMethods; 13 import org.mockitoutil.JUnitResultAssert; 14 import org.mockitoutil.TestBase; 15 16 import static org.junit.Assert.assertEquals; 17 import static org.mockito.Mockito.mock; 18 import static org.mockito.Mockito.times; 19 import static org.mockito.Mockito.verify; 20 import static org.mockito.Mockito.when; 21 22 public class SilentRunnerTest extends TestBase { 23 24 JUnitCore runner = new JUnitCore(); 25 passing_test()26 @Test public void passing_test() { 27 //when 28 Result result = runner.run( 29 SomeFeature.class 30 ); 31 //then 32 JUnitResultAssert.assertThat(result).isSuccessful(); 33 } 34 failing_test()35 @Test public void failing_test() { 36 //when 37 Result result = runner.run( 38 SomeFailingFeature.class 39 ); 40 //then 41 JUnitResultAssert.assertThat(result).fails(1, TooLittleActualInvocations.class); 42 } 43 failing_test_in_constructor()44 @Test public void failing_test_in_constructor() { 45 //when 46 Result result = runner.run( 47 FailsInConstructor.class 48 ); 49 //then 50 JUnitResultAssert.assertThat(result).fails(1, IllegalArgumentException.class); 51 } 52 validates_framework_usage()53 @Test public void validates_framework_usage() { 54 //when 55 Result result = runner.run( 56 UsesFrameworkIncorrectly.class 57 ); 58 //then 59 JUnitResultAssert.assertThat(result).fails(1, "unfinished_stubbing_test_method", UnfinishedStubbingException.class); 60 } 61 62 @Test ignores_unused_stubs()63 public void ignores_unused_stubs() { 64 JUnitCore runner = new JUnitCore(); 65 //when 66 Result result = runner.run(HasUnnecessaryStubs.class); 67 //then 68 JUnitResultAssert.assertThat(result).isSuccessful(); 69 } 70 71 @RunWith(MockitoJUnitRunner.Silent.class) 72 public static class SomeFeature { 73 @Mock List<String> list; some_behavior()74 @Test public void some_behavior() { 75 when(list.get(0)).thenReturn("0"); 76 assertEquals("0", list.get(0)); 77 } 78 } 79 80 @RunWith(MockitoJUnitRunner.Silent.class) 81 public static class SomeFailingFeature { 82 @Mock List<String> list; some_failing_behavior()83 @Test public void some_failing_behavior() { 84 list.clear(); 85 verify(list, times(2)).clear(); 86 } 87 } 88 89 @RunWith(MockitoJUnitRunner.Silent.class) 90 public static class FailsInConstructor { 91 { 92 if (System.currentTimeMillis() > 0) { 93 throw new IllegalArgumentException("Boo!"); 94 } 95 } 96 @Mock List<String> list; some_behavior()97 @Test public void some_behavior() {} 98 } 99 100 @RunWith(MockitoJUnitRunner.Silent.class) 101 public static class UsesFrameworkIncorrectly { 102 @Mock List<?> list; unfinished_stubbing_test_method()103 @Test public void unfinished_stubbing_test_method() { 104 when(list.get(0)); //unfinished stubbing 105 } 106 } 107 108 @RunWith(MockitoJUnitRunner.Silent.class) 109 /** 110 * The test class itself is passing but it has some unnecessary stubs 111 */ 112 public static class HasUnnecessaryStubs { 113 IMethods mock1 = when(mock(IMethods.class).simpleMethod(1)).thenReturn("1").getMock(); 114 IMethods mock2 = when(mock(IMethods.class).simpleMethod(2)).thenReturn("2").getMock(); 115 IMethods mock3 = when(mock(IMethods.class).simpleMethod(3)).thenReturn("3").getMock(); 116 117 @Test usesStub()118 public void usesStub() { 119 assertEquals("1", mock1.simpleMethod(1)); 120 } 121 122 @Test usesStubWithDifferentArg()123 public void usesStubWithDifferentArg() { 124 assertEquals(null, mock2.simpleMethod(200)); 125 assertEquals(null, mock3.simpleMethod(300)); 126 } 127 } 128 } 129