1/*
2 * Copyright (C) 2017 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
17package com.android.dx.mockito.tests;
18
19import android.support.test.runner.AndroidJUnit4;
20
21import org.junit.Ignore;
22import org.junit.Test;
23import org.junit.runner.RunWith;
24import org.mockito.exceptions.base.MockitoException;
25import org.mockito.exceptions.verification.NoInteractionsWanted;
26
27import java.util.ArrayList;
28import java.util.Arrays;
29
30import static org.junit.Assert.assertArrayEquals;
31import static org.junit.Assert.assertEquals;
32import static org.junit.Assert.assertNotNull;
33import static org.junit.Assert.assertNull;
34import static org.junit.Assert.assertTrue;
35import static org.junit.Assert.fail;
36import static org.mockito.ArgumentMatchers.anyString;
37import static org.mockito.ArgumentMatchers.eq;
38import static org.mockito.ArgumentMatchers.isNull;
39import static org.mockito.Mockito.mock;
40import static org.mockito.Mockito.spy;
41import static org.mockito.Mockito.verifyNoMoreInteractions;
42import static org.mockito.Mockito.when;
43
44@RunWith(AndroidJUnit4.class)
45public class GeneralMocking {
46    public static class TestClass {
47        public String returnA() {
48            return "A";
49        }
50
51        public String throwThrowable() throws Throwable {
52            throw new Throwable();
53        }
54
55        public String throwOutOfMemoryError() throws OutOfMemoryError {
56            throw new OutOfMemoryError();
57        }
58
59        public void throwNullPointerException() {
60            throw new NullPointerException();
61        }
62
63        public String concat(String a, String b) {
64            return a + b;
65        }
66    }
67
68    public static class TestSubClass extends TestClass {
69
70    }
71
72    public interface TestInterface {
73        String returnA();
74
75        String concat(String a, String b);
76    }
77
78    @Test
79    public void mockClass() throws Exception {
80        TestClass t = mock(TestClass.class);
81
82        assertNull(t.returnA());
83
84        when(t.returnA()).thenReturn("B");
85        assertEquals("B", t.returnA());
86    }
87
88    @Test
89    public void mockInterface() throws Exception {
90        TestInterface t = mock(TestInterface.class);
91
92        assertNull(t.returnA());
93
94        when(t.returnA()).thenReturn("B");
95        assertEquals("B", t.returnA());
96    }
97
98    @Test
99    public void spyClass() throws Exception {
100        TestClass originalT = new TestClass();
101        TestClass t = spy(originalT);
102
103        assertEquals("A", t.returnA());
104
105        when(t.returnA()).thenReturn("B");
106        assertEquals("B", t.returnA());
107
108        // Wrapped object is not affected by mocking
109        assertEquals("A", originalT.returnA());
110    }
111
112    @Test
113    public void spyNewClass() throws Exception {
114        TestClass t = spy(TestClass.class);
115
116        assertEquals("A", t.returnA());
117
118        when(t.returnA()).thenReturn("B");
119        assertEquals("B", t.returnA());
120    }
121
122    @Test
123    public void verifyAdditionalInvocations() {
124        TestClass t = mock(TestClass.class);
125
126        t.returnA();
127        t.returnA();
128
129        try {
130            verifyNoMoreInteractions(t);
131        } catch (NoInteractionsWanted e) {
132            try {
133                throw new Exception();
134            } catch (Exception here) {
135                // The error message should indicate where the additional invocations have been made
136                assertTrue(e.getMessage(),
137                        e.getMessage().contains(here.getStackTrace()[0].getMethodName()));
138            }
139        }
140    }
141
142    @Test
143    public void spyThrowingMethod() throws Exception {
144        TestClass t = spy(TestClass.class);
145
146        try {
147            t.throwThrowable();
148        } catch (Throwable e) {
149            assertEquals("throwThrowable", e.getStackTrace()[0].getMethodName());
150            return;
151        }
152
153        fail();
154    }
155
156    @Test()
157    public void spyErrorMethod() throws Exception {
158        TestClass t = spy(TestClass.class);
159
160        try {
161            t.throwOutOfMemoryError();
162            fail();
163        } catch (OutOfMemoryError e) {
164            assertEquals("throwOutOfMemoryError", e.getStackTrace()[0].getMethodName());
165        }
166    }
167
168    @Test()
169    public void spyExceptingMethod() throws Exception {
170        TestClass t = spy(TestClass.class);
171
172        try {
173            t.throwNullPointerException();
174            fail();
175        } catch (NullPointerException e) {
176            assertEquals("throwNullPointerException", e.getStackTrace()[0].getMethodName());
177        }
178    }
179
180
181    @Test
182    public void callAbstractRealMethod() throws Exception {
183        TestInterface t = mock(TestInterface.class);
184
185        try {
186            when(t.returnA()).thenCallRealMethod();
187            fail();
188        } catch (MockitoException e) {
189            assertEquals("callAbstractRealMethod", e.getStackTrace()[0].getMethodName());
190        }
191    }
192
193    @Test
194    public void callInterfaceWithoutMatcher() throws Exception {
195        TestInterface t = mock(TestInterface.class);
196
197        when(t.concat("a", "b")).thenReturn("match");
198
199        assertEquals("match", t.concat("a", "b"));
200        assertNull(t.concat("b", "a"));
201    }
202
203    @Test
204    public void callInterfaceWithMatcher() throws Exception {
205        TestInterface t = mock(TestInterface.class);
206
207        when(t.concat(eq("a"), anyString())).thenReturn("match");
208
209        assertEquals("match", t.concat("a", "b"));
210        assertNull(t.concat("b", "a"));
211    }
212
213    @Test
214    public void callInterfaceWithNullMatcher() throws Exception {
215        TestInterface t = mock(TestInterface.class);
216
217        when(t.concat(eq("a"), (String) isNull())).thenReturn("match");
218
219        assertEquals("match", t.concat("a", null));
220        assertNull(t.concat("a", "b"));
221    }
222
223    @Test
224    public void callClassWithoutMatcher() throws Exception {
225        TestClass t = spy(TestClass.class);
226
227        when(t.concat("a", "b")).thenReturn("match");
228
229        assertEquals("match", t.concat("a", "b"));
230        assertEquals("ba", t.concat("b", "a"));
231    }
232
233    @Test
234    public void callClassWithMatcher() throws Exception {
235        TestClass t = spy(TestClass.class);
236
237        when(t.concat(eq("a"), anyString())).thenReturn("match");
238
239        assertEquals("match", t.concat("a", "b"));
240        assertEquals("ba", t.concat("b", "a"));
241    }
242
243    @Test
244    public void callClassWithNullMatcher() throws Exception {
245        TestClass t = spy(TestClass.class);
246
247        when(t.concat(eq("a"), (String) isNull())).thenReturn("match");
248
249        assertEquals("match", t.concat("a", null));
250        assertEquals("ab", t.concat("a", "b"));
251    }
252
253    @Test
254    public void callSubClassWithoutMatcher() throws Exception {
255        TestSubClass t = spy(TestSubClass.class);
256
257        when(t.concat("a", "b")).thenReturn("match");
258
259        assertEquals("match", t.concat("a", "b"));
260        assertEquals("ba", t.concat("b", "a"));
261    }
262
263    @Test
264    public void callSubClassWithMatcher() throws Exception {
265        TestSubClass t = spy(TestSubClass.class);
266
267        when(t.concat(eq("a"), anyString())).thenReturn("match");
268
269        assertEquals("match", t.concat("a", "b"));
270        assertEquals("ba", t.concat("b", "a"));
271    }
272
273    @Test
274    public void callSubClassWithNullMatcher() throws Exception {
275        TestSubClass t = spy(TestSubClass.class);
276
277        when(t.concat(eq("a"), (String) isNull())).thenReturn("match");
278
279        assertEquals("match", t.concat("a", null));
280        assertEquals("ab", t.concat("a", "b"));
281    }
282}
283