• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.dx.mockito.inline.extended.tests;
18 
19 import com.android.dx.mockito.inline.extended.StaticInOrder;
20 
21 import org.junit.Test;
22 import org.mockito.ArgumentCaptor;
23 import org.mockito.MockitoSession;
24 import org.mockito.exceptions.verification.NoInteractionsWanted;
25 import org.mockito.exceptions.verification.VerificationInOrderFailure;
26 
27 import static com.android.dx.mockito.inline.extended.ExtendedMockito.ignoreStubs;
28 import static com.android.dx.mockito.inline.extended.ExtendedMockito.inOrder;
29 import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
30 import static com.android.dx.mockito.inline.extended.ExtendedMockito.staticMockMarker;
31 import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
32 import static com.android.dx.mockito.inline.extended.ExtendedMockito.verifyNoMoreInteractions;
33 import static com.android.dx.mockito.inline.extended.ExtendedMockito.verifyZeroInteractions;
34 import static com.android.dx.mockito.inline.extended.ExtendedMockito.when;
35 import static org.junit.Assert.assertEquals;
36 import static org.junit.Assert.assertNull;
37 import static org.junit.Assert.assertTrue;
38 import static org.junit.Assert.fail;
39 import static org.mockito.ArgumentMatchers.eq;
40 
41 public class VerifyStatic {
42     @Test
verifyMockedStringMethod()43     public void verifyMockedStringMethod() throws Exception {
44         MockitoSession session = mockitoSession().mockStatic(EchoClass.class).startMocking();
45         try {
46             assertNull(EchoClass.echo("marco!"));
47 
48             ArgumentCaptor<String> echoCaptor = ArgumentCaptor.forClass(String.class);
49             verify(() -> {return EchoClass.echo(echoCaptor.capture());});
50             assertEquals("marco!", echoCaptor.getValue());
51 
52             verifyNoMoreInteractions(staticMockMarker(EchoClass.class));
53         } finally {
54             session.finishMocking();
55         }
56     }
57 
58     @Test
verifyMockedVoidMethod()59     public void verifyMockedVoidMethod() throws Exception {
60         MockitoSession session = mockitoSession().mockStatic(ConsumeClass.class).startMocking();
61         try {
62             ConsumeClass.consume("donut");
63 
64             ArgumentCaptor<String> yumCaptor = ArgumentCaptor.forClass(String.class);
65             verify(() -> ConsumeClass.consume(yumCaptor.capture()));
66 
67             verifyNoMoreInteractions(staticMockMarker(ConsumeClass.class));
68         } finally {
69             session.finishMocking();
70         }
71     }
72 
73     @Test
verifyWithTwoMocks()74     public void verifyWithTwoMocks() throws Exception {
75         MockitoSession session = mockitoSession().mockStatic(EchoClass.class)
76                 .mockStatic(ConsumeClass.class).startMocking();
77         try {
78             ConsumeClass.consume("donut");
79             assertNull(EchoClass.echo("marco!"));
80 
81             ArgumentCaptor<String> yumCaptor = ArgumentCaptor.forClass(String.class);
82             verify(() -> ConsumeClass.consume(yumCaptor.capture()));
83 
84             ArgumentCaptor<String> echoCaptor = ArgumentCaptor.forClass(String.class);
85             verify(() -> {return EchoClass.echo(echoCaptor.capture());});
86             assertEquals("marco!", echoCaptor.getValue());
87 
88             verifyNoMoreInteractions(staticMockMarker(ConsumeClass.class));
89         } finally {
90             session.finishMocking();
91         }
92     }
93 
94     @Test
verifySpiedStringMethod()95     public void verifySpiedStringMethod() throws Exception {
96         MockitoSession session = mockitoSession().spyStatic(EchoClass.class).startMocking();
97         try {
98             assertEquals("marco!", EchoClass.echo("marco!"));
99 
100             ArgumentCaptor<String> echoCaptor = ArgumentCaptor.forClass(String.class);
101             verify(() -> {return EchoClass.echo(echoCaptor.capture());});
102             assertEquals("marco!", echoCaptor.getValue());
103 
104             verifyNoMoreInteractions(staticMockMarker(EchoClass.class));
105         } finally {
106             session.finishMocking();
107         }
108     }
109 
110     @Test
verifyInOrder()111     public void verifyInOrder() throws Exception {
112         MockitoSession session = mockitoSession().mockStatic(EchoClass.class).mockStatic
113                 (ConsumeClass.class).startMocking();
114         try {
115             EchoClass.echo("marco!");
116             ConsumeClass.consume("donuts");
117             ConsumeClass.consume("nougat");
118             EchoClass.echo("polo");
119 
120             StaticInOrder echoInOrder = inOrder(staticMockMarker(EchoClass.class));
121             echoInOrder.verify(() -> EchoClass.echo(eq("marco!")));
122             echoInOrder.verify(() -> EchoClass.echo(eq("polo")));
123             echoInOrder.verifyNoMoreInteractions();
124 
125             StaticInOrder consumeInOrder = inOrder(staticMockMarker(ConsumeClass.class));
126             consumeInOrder.verify(() -> ConsumeClass.consume(eq("donuts")));
127             consumeInOrder.verify(() -> ConsumeClass.consume(eq("nougat")));
128             consumeInOrder.verifyNoMoreInteractions();
129 
130             StaticInOrder combinedInOrder = inOrder(staticMockMarker(EchoClass.class,
131                     ConsumeClass.class));
132             combinedInOrder.verify(() -> EchoClass.echo(eq("marco!")));
133             combinedInOrder.verify(() -> ConsumeClass.consume(eq("donuts")));
134             combinedInOrder.verify(() -> ConsumeClass.consume(eq("nougat")));
135             combinedInOrder.verify(() -> EchoClass.echo(eq("polo")));
136             combinedInOrder.verifyNoMoreInteractions();
137         } finally {
138             session.finishMocking();
139         }
140     }
141 
142     @Test(expected = VerificationInOrderFailure.class)
verifyBadOrder()143     public void verifyBadOrder() throws Exception {
144         MockitoSession session = mockitoSession().mockStatic(EchoClass.class).startMocking();
145         try {
146             EchoClass.echo("marco!");
147             EchoClass.echo("polo");
148 
149             StaticInOrder echoInOrder = inOrder(staticMockMarker(EchoClass.class));
150             echoInOrder.verify(() -> EchoClass.echo(eq("polo")));
151             echoInOrder.verify(() -> EchoClass.echo(eq("marco!")));
152         } finally {
153             session.finishMocking();
154         }
155     }
156 
157     @Test
verifyBadMatcher()158     public void verifyBadMatcher() throws Exception {
159         MockitoSession session = mockitoSession().mockStatic(EchoClass.class).startMocking();
160         try {
161             EchoClass.echo("marco!");
162             EchoClass.echo("polo");
163 
164             StaticInOrder echoInOrder = inOrder(staticMockMarker(EchoClass.class));
165             echoInOrder.verify(() -> EchoClass.echo(eq("marco!")));
166 
167             try {
168                 echoInOrder.verify(() -> EchoClass.echo(eq("badMarker")));
169                 fail();
170             } catch (VerificationInOrderFailure e) {
171                 assertTrue(e.getMessage(), e.getMessage().contains("badMarker"));
172             }
173         } finally {
174             session.finishMocking();
175         }
176     }
177 
178     @Test(expected = NoInteractionsWanted.class)
zeroInvocationsThrowsIfThereWasAnInvocation()179     public void zeroInvocationsThrowsIfThereWasAnInvocation() throws Exception {
180         MockitoSession session = mockitoSession().mockStatic(EchoClass.class).startMocking();
181         try {
182             EchoClass.echo("marco!");
183             verifyZeroInteractions(staticMockMarker(EchoClass.class));
184             fail();
185         } finally {
186             session.finishMocking();
187         }
188     }
189 
190     @Test
verifyWithIgnoreStubs()191     public void verifyWithIgnoreStubs() throws Exception {
192         MockitoSession session = mockitoSession().spyStatic(EchoClass.class).startMocking();
193         try {
194             // 'ignoreStubs' only ignore stubs
195             when(EchoClass.echo("marco!")).thenReturn("polo");
196             assertEquals("polo", EchoClass.echo("marco!"));
197             assertEquals("echo", EchoClass.echo("echo"));
198 
199             verify(() -> {return EchoClass.echo(eq("echo"));});
200             verifyNoMoreInteractions(ignoreStubs(staticMockMarker(EchoClass.class)));
201         } finally {
202             session.finishMocking();
203         }
204     }
205 
206     private static class EchoClass {
echo(String echo)207         static final String echo(String echo) {
208             return echo;
209         }
210     }
211 
212     private static class ConsumeClass {
consume(String yum)213         static final void consume(String yum) {
214             // empty
215         }
216     }
217 }
218