How to use method of org.easymock.tests2.EasyMockClassExtensionTest class

Best Easymock code snippet using org.easymock.tests2.EasyMockClassExtensionTest.

Run Easymock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/*
2 * Copyright 2001-2021 the original author or authors.
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 */
16package org.easymock.tests2;
17
18import org.easymock.ConstructorArgs;
19import org.easymock.EasyMock;
20import org.easymock.IMockBuilder;
21import org.easymock.internal.EasyMockProperties;
22import org.easymock.tests2.MocksControlTest.A;
23import org.junit.Test;
24
25import java.lang.reflect.Method;
26import java.math.BigDecimal;
27import java.util.ArrayList;
28import java.util.Arrays;
29import java.util.List;
30
31import static org.easymock.EasyMock.*;
32import static org.junit.Assert.*;
33
34/**
35 * @author Henri Tremblay
36 */
37public class EasyMockClassExtensionTest {
38
39    private static class ParamEntry {
40        Class<?>[] types;
41
42        Object[] values;
43
44        ParamEntry(Class<?>[] types, Object[] values) {
45            this.types = types;
46            this.values = values;
47        }
48
49        boolean isNamed() {
50            return types[0] == String.class;
51        }
52
53        boolean isConstructorCalled() {
54            return Arrays.asList(types).contains(ConstructorArgs.class);
55        }
56
57        A getMock(String methodName) throws Exception {
58            Method m = EasyMock.class.getMethod(methodName, types);
59            return (A) m.invoke(null, values);
60        }
61
62        public void test(A mock) {
63            if (isNamed()) {
64                testNamed(mock);
65            }
66            if (isConstructorCalled()) {
67                testPartial_ConstructorCalled(mock);
68            } else {
69                testPartial_NoConstructorCalled(mock);
70            }
71        }
72    }
73
74    /** Types of all method flavors */
75    private static final Class<?>[][] PARAMETER_TYPES = new Class<?>[][] {
76            new Class[] { Class.class }, //
77            new Class[] { String.class, Class.class } //
78    };
79
80    /** Values to pass to each method call */
81    private static final Object[][] PARAMETER_VALUES;
82
83    /** All 6 flavors of method calls */
84    private static final ParamEntry[] PARAMETERS = new ParamEntry[PARAMETER_TYPES.length];
85
86    static {
87
88        Method[] methods;
89        try {
90            methods = new Method[] { A.class.getMethod("add", Integer.TYPE), A.class.getMethod("toString") };
91        } catch (NoSuchMethodException e) {
92            throw new RuntimeException(e);
93        }
94        ConstructorArgs args;
95        try {
96            args = new ConstructorArgs(A.class.getConstructor(Integer.TYPE), 3);
97        } catch (SecurityException | NoSuchMethodException e) {
98            throw new RuntimeException(e);
99        }
100
101        PARAMETER_VALUES = new Object[][] {
102                new Object[] { A.class }, //
103                new Object[] { "myMock", A.class } //
104        };
105
106        for (int i = 0; i < PARAMETERS.length; i++) {
107            PARAMETERS[i] = new ParamEntry(PARAMETER_TYPES[i], PARAMETER_VALUES[i]);
108        }
109    }
110
111    @Test
112    public void testDisablingClassMocking() {
113        EasyMockProperties.getInstance().setProperty(DISABLE_CLASS_MOCKING, Boolean.TRUE.toString());
114        try {
115            ArrayList<?> list = createMock(ArrayList.class);
116            fail("Class mocking should be disabled");
117        } catch (IllegalArgumentException e) {
118            assertEquals("Class mocking is currently disabled. Change " + EasyMock.DISABLE_CLASS_MOCKING
119                    + " to true do modify this behavior", e.getMessage());
120        } finally {
121            EasyMockProperties.getInstance().setProperty(DISABLE_CLASS_MOCKING, null);
122        }
123    }
124
125    @Test
126    public void testClassMocking() {
127        ArrayList<?> list = createMock(ArrayList.class);
128        testList(list);
129    }
130
131    @Test
132    public void testInterfaceMocking() {
133        List<?> list = createMock(List.class);
134        testList(list);
135    }
136
137    private void testList(List<?> list) {
138        expect(list.size()).andReturn(3);
139        replay(list);
140        assertEquals(3, list.size());
141        verify(list);
142    }
143
144    @Test
145    public void testResetReplay() {
146        ArrayList<?> list = createStrictMock(ArrayList.class);
147        expect(list.size()).andReturn(3);
148        reset(list);
149        expect(list.size()).andReturn(1);
150        replay(list);
151        assertEquals(1, list.size());
152        verify(list);
153    }
154
155    @Test
156    public void testResetTo() {
157        ArrayList<?> list = createMock(ArrayList.class);
158        // Just to make sure the all can be called on a mock
159        resetToNice(list);
160        resetToStrict(list);
161        resetToDefault(list);
162    }
163
164    @Test
165    public void testMakeThreadSafe() {
166        ArrayList<?> list = createMock(ArrayList.class);
167        // Just to make sure the all can be called on a mock
168        makeThreadSafe(list, true);
169    }
170
171    @Test
172    public void testVarargs() {
173        ArrayList<?> list2 = createStrictMock(ArrayList.class);
174        ArrayList<?> list1 = createStrictMock(ArrayList.class);
175
176        expect(list1.size()).andReturn(1);
177        expect(list2.size()).andReturn(2);
178        reset(list1, list2);
179
180        expect(list1.size()).andReturn(3);
181        expect(list2.size()).andReturn(4);
182        replay(list1, list2);
183
184        assertEquals(3, list1.size());
185        assertEquals(4, list2.size());
186
187        verify(list1, list2);
188    }
189
190    @SuppressWarnings("unchecked")
191    @Test
192    public void testCheckOrder() {
193        ArrayList<Integer> list = createStrictMock(ArrayList.class);
194        checkOrder(list, false);
195        expect(list.add(1)).andReturn(true);
196        expect(list.add(3)).andReturn(true);
197        replay(list);
198        list.add(3);
199        list.add(1);
200        verify(list);
201    }
202
203    @SuppressWarnings("unchecked")
204    @Test
205    public void testStrictMock_Partial() {
206        List<Integer> list = createMockBuilder(ArrayList.class).addMockedMethod("add",
207                Object.class).createStrictMock();
208
209        expect(list.add(1)).andReturn(true);
210        expect(list.add(2)).andReturn(true);
211
212        replay(list);
213
214        assertTrue(list.isEmpty());
215
216        try {
217            list.add(2);
218            fail();
219        } catch (AssertionError e) {
220        }
221    }
222
223    @SuppressWarnings("unchecked")
224    @Test
225    public void testMock_Partial() {
226        ArrayList<Integer> list = createMockBuilder(ArrayList.class).addMockedMethod("add",
227                Object.class).createMock();
228
229        expect(list.add(1)).andReturn(true);
230        expect(list.add(2)).andReturn(true);
231
232        replay(list);
233
234        assertTrue(list.isEmpty());
235
236        list.add(2);
237        list.add(1);
238
239        verify(list);
240    }
241
242    @Test
243    public void testNiceMock_Partial() {
244        ArrayList<?> list = createMockBuilder(ArrayList.class).addMockedMethod("get").createNiceMock();
245
246        replay(list);
247
248        assertNull(list.get(0));
249        assertTrue(list.isEmpty());
250    }
251
252    @SuppressWarnings("unchecked")
253    @Test
254    public void testCompare() {
255        BigDecimal expected = new BigDecimal("15.6");
256        BigDecimal actual = new BigDecimal("15.60");
257
258        ArrayList<BigDecimal> list = createMock(ArrayList.class);
259        expect(list.add(cmpEq(expected))).andReturn(true);
260
261        replay(list);
262
263        list.add(actual);
264
265        verify(list);
266    }
267
268    @SuppressWarnings("unchecked")
269    @Test
270    public void testNamedMock() throws Exception {
271        ArrayList<BigDecimal> list = createMock("mockName", ArrayList.class);
272        assertEquals("mockName", list.toString());
273        list = createStrictMock("mockName", ArrayList.class);
274        assertEquals("mockName", list.toString());
275        list = createNiceMock("mockName", ArrayList.class);
276        assertEquals("mockName", list.toString());
277
278        // Note that toString needs to be mocked if you want EasyMock default
279        // toString() behavior
280        Method m = ArrayList.class.getMethod("toString", (Class<?>[]) null);
281
282        list = createMockBuilder(ArrayList.class).addMockedMethod(m).createMock("mockName");
283        assertEquals("mockName", list.toString());
284        list = createMockBuilder(ArrayList.class).addMockedMethod(m).createStrictMock("mockName");
285        assertEquals("mockName", list.toString());
286        list = createMockBuilder(ArrayList.class).addMockedMethod(m).createNiceMock("mockName");
287        assertEquals("mockName", list.toString());
288    }
289
290    @Test
291    public void testStrictMock() throws Exception {
292        for (ParamEntry p : PARAMETERS) {
293            A mock = p.getMock("createStrictMock");
294            p.test(mock);
295            testStrict(mock);
296        }
297    }
298
299    @Test
300    public void testNormalMock() throws Exception {
301        for (ParamEntry p : PARAMETERS) {
302            A mock = p.getMock("createMock");
303            p.test(mock);
304            testNormal(mock);
305        }
306    }
307
308    @Test
309    public void testNiceMock() throws Exception {
310        for (ParamEntry p : PARAMETERS) {
311            A mock = p.getMock("createNiceMock");
312            p.test(mock);
313            testNice(mock);
314        }
315    }
316
317    @Test
318    public void testCreateMockBuilder() {
319        IMockBuilder<A> builder = createMockBuilder(A.class);
320        A a = builder.withConstructor(int.class).withArgs(2).createMock();
321        assertEquals(2, a.i);
322    }
323
324    // 3 mock types
325
326    private static void testStrict(A mock) {
327        reset(mock); // just in case we are not in a stable state
328        expect(mock.add(1)).andReturn(true);
329        expect(mock.add(2)).andReturn(true);
330        replay(mock);
331        try {
332            mock.add(2);
333            fail("Should be ordered");
334        } catch (AssertionError e) {
335        }
336    }
337
338    private static void testNormal(A mock) {
339        reset(mock); // just in case we are not in a stable state
340        expect(mock.add(1)).andReturn(true);
341        expect(mock.add(2)).andReturn(true);
342        replay(mock);
343        // unordered
344        mock.add(2);
345        mock.add(1);
346        // but not nice
347        try {
348            mock.add(3);
349            fail("Should be ordered");
350        } catch (AssertionError e) {
351        }
352    }
353
354    private static void testNice(A mock) {
355        reset(mock); // just in case we are not in a stable state
356        replay(mock);
357        assertFalse(mock.add(2));
358        verify(mock);
359    }
360
361    // call flavors
362
363    private static void testNamed(A mock) {
364        assertEquals("myMock", mock.toString());
365    }
366
367    private static void testPartial_NoConstructorCalled(A mock) {
368        // not really nice since I'm looking at the inner implementation
369        assertEquals(0, mock.i);
370    }
371
372    private static void testPartial_ConstructorCalled(A mock) {
373        assertEquals(3, mock.i);
374    }
375}
376
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)