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