1 /* 2 * Copyright (c) 2018 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockitousage.stubbing; 6 7 import org.junit.After; 8 import org.junit.Test; 9 import org.mockito.Mock; 10 import org.mockito.MockitoSession; 11 import org.mockito.StateMaster; 12 import org.mockito.exceptions.base.MockitoException; 13 import org.mockito.internal.framework.DefaultMockitoSession; 14 import org.mockito.internal.util.SimpleMockitoLogger; 15 import org.mockito.quality.Strictness; 16 import org.mockitousage.IMethods; 17 18 import static java.util.Collections.singletonList; 19 import static org.junit.Assert.assertEquals; 20 import static org.mockito.BDDMockito.given; 21 import static org.mockito.Mockito.verify; 22 import static org.mockitoutil.TestBase.filterLineNo; 23 24 public class StubbingWarningsTest { 25 26 private static final String TEST_NAME = "test.name"; 27 28 @Mock IMethods mock; 29 30 SimpleMockitoLogger logger = new SimpleMockitoLogger(); 31 MockitoSession mockito = new DefaultMockitoSession(singletonList((Object) this), TEST_NAME, Strictness.WARN, logger); 32 after()33 @After public void after() { 34 StateMaster stateMaster = new StateMaster(); 35 stateMaster.reset(); 36 stateMaster.clearMockitoListeners(); 37 } 38 few_interactions()39 @Test public void few_interactions() throws Throwable { 40 //when 41 mock.simpleMethod(100); 42 mock.otherMethod(); 43 44 //expect no exception 45 mockito.finishMocking(); 46 logger.assertEmpty(); 47 } 48 stubbing_used()49 @Test public void stubbing_used() throws Throwable { 50 //when 51 given(mock.simpleMethod(100)).willReturn("100"); 52 mock.simpleMethod(100); 53 54 //then 55 mockito.finishMocking(); 56 logger.assertEmpty(); 57 } 58 unused_stubbed_is_not_implicitly_verified()59 @Test public void unused_stubbed_is_not_implicitly_verified() throws Throwable { 60 //when 61 given(mock.simpleMethod(100)).willReturn("100"); 62 mock.simpleMethod(100); // <- stubbing is used 63 mock.simpleMethod(200); // <- other method should not generate arg mismatch 64 65 //then 66 mockito.finishMocking(); 67 logger.assertEmpty(); 68 } 69 stubbing_argument_mismatch()70 @Test public void stubbing_argument_mismatch() throws Throwable { 71 //when 72 given(mock.simpleMethod(100)).willReturn("100"); 73 mock.simpleMethod(200); 74 75 mockito.finishMocking(); 76 77 //TODO - currently we warn about "Unused" instead of "Arg mismatch" below 78 //because it was simpler to implement. This can be improved given we put priority to improve the warnings. 79 //then 80 assertEquals(filterLineNo( 81 "[MockitoHint] " + TEST_NAME + " (see javadoc for MockitoHint):\n" + 82 "[MockitoHint] 1. Unused -> at org.mockitousage.stubbing.StubbingWarningsTest.stubbing_argument_mismatch(StubbingWarningsTest.java:0)\n"), 83 filterLineNo(logger.getLoggedInfo())); 84 } 85 unused_stubbing()86 @Test public void unused_stubbing() throws Throwable { 87 //when 88 given(mock.simpleMethod(100)).willReturn("100"); 89 90 mockito.finishMocking(); 91 92 //then 93 assertEquals(filterLineNo( 94 "[MockitoHint] " + TEST_NAME + " (see javadoc for MockitoHint):\n" + 95 "[MockitoHint] 1. Unused -> at org.mockitousage.stubbing.StubbingWarningsTest.unused_stubbing(StubbingWarningsTest.java:0)\n"), 96 filterLineNo(logger.getLoggedInfo())); 97 } 98 99 @SuppressWarnings({"MockitoUsage", "CheckReturnValue"}) unfinished_verification_without_throwable()100 @Test(expected = MockitoException.class) public void unfinished_verification_without_throwable() throws Throwable { 101 //when 102 verify(mock); 103 104 mockito.finishMocking(); 105 } 106 107 @SuppressWarnings({"MockitoUsage", "CheckReturnValue"}) unfinished_verification_with_throwable()108 @Test public void unfinished_verification_with_throwable() throws Throwable { 109 //when 110 verify(mock); 111 112 mockito.finishMocking(new AssertionError()); 113 114 // then 115 logger.assertEmpty(); 116 } 117 } 118