• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     @Test
makeSureStateIsValidatedInTheVeryFirstTestThanksToTheRunner()36     public void makeSureStateIsValidatedInTheVeryFirstTestThanksToTheRunner() {
37         //mess up the state:
38         verify(mock);
39     }
40 
41     @Test
shouldSayWantedButNotInvoked()42     public void shouldSayWantedButNotInvoked() {
43         verify(mock).otherMethod();
44     }
45 
46     @Test
shouldPointOutInteractionsOnMockWhenOrdinaryVerificationFails()47     public void shouldPointOutInteractionsOnMockWhenOrdinaryVerificationFails() {
48         mock.otherMethod();
49         mock.booleanObjectReturningMethod();
50 
51         verify(mock).simpleMethod();
52     }
53 
54     @Test
shouldShowActualAndExpected()55     public void shouldShowActualAndExpected() {
56         mock.simpleMethod("blah");
57         verify(mock).simpleMethod();
58     }
59 
60     @Test
shouldSayTooLittleInvocations()61     public void shouldSayTooLittleInvocations() {
62         mock.simpleMethod();
63         verify(mock, times(2)).simpleMethod();
64     }
65 
66     @Test
shouldSayTooManyInvocations()67     public void shouldSayTooManyInvocations() {
68         mock.simpleMethod();
69         mock.simpleMethod();
70         verify(mock, times(1)).simpleMethod();
71     }
72 
73     @Test
shouldSayWantedButNotInvokedInOrder()74     public void shouldSayWantedButNotInvokedInOrder() {
75         mock.simpleMethod();
76         mock.otherMethod();
77         InOrder inOrder = inOrder(mock);
78         inOrder.verify(mock).otherMethod();
79         inOrder.verify(mock).simpleMethod();
80     }
81 
82     @Test
shouldSayTooLittleInvocationsInOrder()83     public void shouldSayTooLittleInvocationsInOrder() {
84         mock.simpleMethod();
85         mock.otherMethod();
86         mock.otherMethod();
87 
88         InOrder inOrder = inOrder(mock);
89         inOrder.verify(mock).simpleMethod();
90         inOrder.verify(mock, times(3)).otherMethod();
91     }
92 
93     @Test
shouldSayTooManyInvocationsInOrder()94     public void shouldSayTooManyInvocationsInOrder() {
95         mock.otherMethod();
96         mock.otherMethod();
97 
98         InOrder inOrder = inOrder(mock);
99         inOrder.verify(mock, times(1)).otherMethod();
100     }
101 
102     @Test
shouldSayNeverWantedButInvokedHere()103     public void shouldSayNeverWantedButInvokedHere() {
104         mock.otherMethod();
105 
106         verify(mock, never()).otherMethod();
107     }
108 
109     @Test
shouldSayTooLittleInvocationsInAtLeastModeInOrder()110     public void shouldSayTooLittleInvocationsInAtLeastModeInOrder() {
111         mock.simpleMethod();
112 
113         InOrder inOrder = inOrder(mock);
114         inOrder.verify(mock, atLeast(2)).simpleMethod();
115     }
116 
117     @Test
shouldSayTooLittleInvocationsInAtLeastMode()118     public void shouldSayTooLittleInvocationsInAtLeastMode() {
119         mock.simpleMethod();
120 
121         verify(mock, atLeast(2)).simpleMethod();
122     }
123 
124     @Test
shouldSayNoMoreInteractions()125     public void shouldSayNoMoreInteractions() {
126         mock.simpleMethod();
127 
128         verifyNoMoreInteractions(mock);
129     }
130 
131     @Test
shouldSayUnstubbedMethodWasInvokedHere()132     public void shouldSayUnstubbedMethodWasInvokedHere() {
133         mock = mock(IMethods.class, RETURNS_SMART_NULLS);
134 
135         IMethods m = mock.iMethodsReturningMethod();
136 
137         m.simpleMethod();
138     }
139 
140     @Test
shouldPointOutUnfinishedStubbing()141     public void shouldPointOutUnfinishedStubbing() {
142         when(mock.simpleMethod());
143 
144         verify(mock).simpleMethod();
145     }
146 
147     @Test
shouldMentionFinalAndObjectMethodsWhenMissingMockCall()148     public void shouldMentionFinalAndObjectMethodsWhenMissingMockCall() {
149         when("".equals(null)).thenReturn(false);
150     }
151 
152     @Test
shouldMentionFinalAndObjectMethodsWhenVerifying()153     public void shouldMentionFinalAndObjectMethodsWhenVerifying() {
154         verify(mock).equals(null);
155         verify(mock).simpleMethod();
156     }
157 
158     @Test
shouldMentionFinalAndObjectMethodsWhenMisplacedArgumentMatcher()159     public void shouldMentionFinalAndObjectMethodsWhenMisplacedArgumentMatcher() {
160         when(mock.equals(anyObject())).thenReturn(false);
161     }
162 
163     @Test
shouldShowExampleOfCorrectArgumentCapturing()164     public void shouldShowExampleOfCorrectArgumentCapturing() {
165         ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
166         argument.capture();
167         argument.getValue();
168     }
169 
170     @Test
shouldScreamWhenNullPassedInsteadOfAnInterface()171     public void shouldScreamWhenNullPassedInsteadOfAnInterface() {
172         mock(IMethods.class, withSettings().extraInterfaces(List.class, null));
173     }
174 
175     @Test
shouldScreamWhenNonInterfacePassed()176     public void shouldScreamWhenNonInterfacePassed() {
177         mock(IMethods.class, withSettings().extraInterfaces(LinkedList.class));
178     }
179 
180     @Test
shouldScreamWhenExtraIsTheSame()181     public void shouldScreamWhenExtraIsTheSame() {
182         mock(IMethods.class, withSettings().extraInterfaces(IMethods.class));
183     }
184 
185     @Test
shouldScreamWhenExtraInterfacesEmpty()186     public void shouldScreamWhenExtraInterfacesEmpty() {
187         mock(IMethods.class, withSettings().extraInterfaces());
188     }
189 
190     @Test
shouldScreamWhenExtraInterfacesIsANullArray()191     public void shouldScreamWhenExtraInterfacesIsANullArray() {
192         mock(IMethods.class, withSettings().extraInterfaces((Class<?>[]) null));
193     }
194 
195     @Test
shouldMentionSpiesWhenVoidMethodIsToldToReturnValue()196     public void shouldMentionSpiesWhenVoidMethodIsToldToReturnValue() {
197         List list = mock(List.class);
198         doReturn("foo").when(list).clear();
199     }
200 }
201