1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 6 package org.mockitousage.stacktrace; 7 8 import org.junit.Before; 9 import org.junit.Ignore; 10 import org.junit.Test; 11 import org.junit.runner.RunWith; 12 import org.mockito.ArgumentCaptor; 13 import org.mockito.InOrder; 14 import org.mockito.Mock; 15 import org.mockito.junit.MockitoJUnitRunner; 16 import org.mockitousage.IMethods; 17 import org.mockitoutil.TestBase; 18 19 import java.util.LinkedList; 20 import java.util.List; 21 22 import static org.mockito.Mockito.*; 23 24 @Ignore 25 @RunWith(MockitoJUnitRunner.class) 26 public class ModellingDescriptiveMessagesTest extends TestBase { 27 28 @Mock private IMethods mock; 29 30 @Before cleanStackTrace()31 public void cleanStackTrace() { 32 super.makeStackTracesClean(); 33 } 34 35 @SuppressWarnings({"MockitoUsage", "CheckReturnValue"}) 36 @Test makeSureStateIsValidatedInTheVeryFirstTestThanksToTheRunner()37 public void makeSureStateIsValidatedInTheVeryFirstTestThanksToTheRunner() { 38 //mess up the state: 39 verify(mock); 40 } 41 42 @Test shouldSayWantedButNotInvoked()43 public void shouldSayWantedButNotInvoked() { 44 verify(mock).otherMethod(); 45 } 46 47 @Test shouldPointOutInteractionsOnMockWhenOrdinaryVerificationFails()48 public void shouldPointOutInteractionsOnMockWhenOrdinaryVerificationFails() { 49 mock.otherMethod(); 50 mock.booleanObjectReturningMethod(); 51 52 verify(mock).simpleMethod(); 53 } 54 55 @Test shouldShowActualAndExpected()56 public void shouldShowActualAndExpected() { 57 mock.simpleMethod("blah"); 58 verify(mock).simpleMethod(); 59 } 60 61 @Test shouldSayTooLittleInvocations()62 public void shouldSayTooLittleInvocations() { 63 mock.simpleMethod(); 64 verify(mock, times(2)).simpleMethod(); 65 } 66 67 @Test shouldSayTooManyInvocations()68 public void shouldSayTooManyInvocations() { 69 mock.simpleMethod(); 70 mock.simpleMethod(); 71 verify(mock, times(1)).simpleMethod(); 72 } 73 74 @Test shouldSayWantedButNotInvokedInOrder()75 public void shouldSayWantedButNotInvokedInOrder() { 76 mock.simpleMethod(); 77 mock.otherMethod(); 78 InOrder inOrder = inOrder(mock); 79 inOrder.verify(mock).otherMethod(); 80 inOrder.verify(mock).simpleMethod(); 81 } 82 83 @Test shouldSayTooLittleInvocationsInOrder()84 public void shouldSayTooLittleInvocationsInOrder() { 85 mock.simpleMethod(); 86 mock.otherMethod(); 87 mock.otherMethod(); 88 89 InOrder inOrder = inOrder(mock); 90 inOrder.verify(mock).simpleMethod(); 91 inOrder.verify(mock, times(3)).otherMethod(); 92 } 93 94 @Test shouldSayTooManyInvocationsInOrder()95 public void shouldSayTooManyInvocationsInOrder() { 96 mock.otherMethod(); 97 mock.otherMethod(); 98 99 InOrder inOrder = inOrder(mock); 100 inOrder.verify(mock, times(1)).otherMethod(); 101 } 102 103 @Test shouldSayNeverWantedButInvokedHere()104 public void shouldSayNeverWantedButInvokedHere() { 105 mock.otherMethod(); 106 107 verify(mock, never()).otherMethod(); 108 } 109 110 @Test shouldSayTooLittleInvocationsInAtLeastModeInOrder()111 public void shouldSayTooLittleInvocationsInAtLeastModeInOrder() { 112 mock.simpleMethod(); 113 114 InOrder inOrder = inOrder(mock); 115 inOrder.verify(mock, atLeast(2)).simpleMethod(); 116 } 117 118 @Test shouldSayTooLittleInvocationsInAtLeastMode()119 public void shouldSayTooLittleInvocationsInAtLeastMode() { 120 mock.simpleMethod(); 121 122 verify(mock, atLeast(2)).simpleMethod(); 123 } 124 125 @Test shouldSayNoMoreInteractions()126 public void shouldSayNoMoreInteractions() { 127 mock.simpleMethod(); 128 129 verifyNoMoreInteractions(mock); 130 } 131 132 @Test shouldSayUnstubbedMethodWasInvokedHere()133 public void shouldSayUnstubbedMethodWasInvokedHere() { 134 mock = mock(IMethods.class, RETURNS_SMART_NULLS); 135 136 IMethods m = mock.iMethodsReturningMethod(); 137 138 m.simpleMethod(); 139 } 140 141 @SuppressWarnings({"MockitoUsage", "CheckReturnValue"}) 142 @Test shouldPointOutUnfinishedStubbing()143 public void shouldPointOutUnfinishedStubbing() { 144 when(mock.simpleMethod()); 145 146 verify(mock).simpleMethod(); 147 } 148 149 @Test shouldMentionFinalAndObjectMethodsWhenMissingMockCall()150 public void shouldMentionFinalAndObjectMethodsWhenMissingMockCall() { 151 when("".equals(null)).thenReturn(false); 152 } 153 154 @Test shouldMentionFinalAndObjectMethodsWhenVerifying()155 public void shouldMentionFinalAndObjectMethodsWhenVerifying() { 156 verify(mock).equals(null); 157 verify(mock).simpleMethod(); 158 } 159 160 @Test shouldMentionFinalAndObjectMethodsWhenMisplacedArgumentMatcher()161 public void shouldMentionFinalAndObjectMethodsWhenMisplacedArgumentMatcher() { 162 when(mock.equals(anyObject())).thenReturn(false); 163 } 164 165 @Test shouldShowExampleOfCorrectArgumentCapturing()166 public void shouldShowExampleOfCorrectArgumentCapturing() { 167 ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class); 168 argument.capture(); 169 argument.getValue(); 170 } 171 172 @SuppressWarnings({"CheckReturnValue", "MockitoUsage"}) 173 @Test shouldScreamWhenNullPassedInsteadOfAnInterface()174 public void shouldScreamWhenNullPassedInsteadOfAnInterface() { 175 mock(IMethods.class, withSettings().extraInterfaces(List.class, null)); 176 } 177 178 @SuppressWarnings({"CheckReturnValue", "MockitoUsage"}) 179 @Test shouldScreamWhenNonInterfacePassed()180 public void shouldScreamWhenNonInterfacePassed() { 181 mock(IMethods.class, withSettings().extraInterfaces(LinkedList.class)); 182 } 183 184 @SuppressWarnings({"CheckReturnValue", "MockitoUsage"}) 185 @Test shouldScreamWhenExtraIsTheSame()186 public void shouldScreamWhenExtraIsTheSame() { 187 mock(IMethods.class, withSettings().extraInterfaces(IMethods.class)); 188 } 189 190 @SuppressWarnings({"CheckReturnValue", "MockitoUsage"}) 191 @Test shouldScreamWhenExtraInterfacesEmpty()192 public void shouldScreamWhenExtraInterfacesEmpty() { 193 mock(IMethods.class, withSettings().extraInterfaces()); 194 } 195 196 @SuppressWarnings({"CheckReturnValue", "MockitoUsage"}) 197 @Test shouldScreamWhenExtraInterfacesIsANullArray()198 public void shouldScreamWhenExtraInterfacesIsANullArray() { 199 mock(IMethods.class, withSettings().extraInterfaces((Class<?>[]) null)); 200 } 201 202 @Test shouldMentionSpiesWhenVoidMethodIsToldToReturnValue()203 public void shouldMentionSpiesWhenVoidMethodIsToldToReturnValue() { 204 List list = mock(List.class); 205 doReturn("foo").when(list).clear(); 206 } 207 } 208