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

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

Run Easymock automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright 2001-2022 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.EasyMockSupport;
20import org.easymock.IMocksControl;
21import org.easymock.MockType;
22import org.easymock.internal.MockBuilder;
23import org.easymock.internal.MocksControl;
24import org.junit.Before;
25import org.junit.Test;
26
27import java.util.ArrayList;
28
29import static org.easymock.EasyMock.*;
30import static org.junit.Assert.*;
31
32/**
33 * @author Henri Tremblay
34 */
35public class MockBuilderTest {
36
37    private static class A {
38        public final void foo(String s) {
39        }
40    }
41
42    private MockBuilder<ArrayList<String>> builder;
43
44    private ArrayList<String> mock;
45
46    @SuppressWarnings({ "rawtypes", "unchecked" })
47    @Before
48    public void setUp() {
49        builder = new MockBuilder(ArrayList.class);
50    }
51
52    @Test
53    public void testAddMockedMethod() throws NoSuchMethodException {
54        builder.addMockedMethod(ArrayList.class.getMethod("size"))
55                .addMockedMethod("contains")
56                .addMockedMethod("add", Object.class)
57                .addMockedMethods("clear", "isEmpty")
58                .addMockedMethods(ArrayList.class.getMethod("get", int.class),
59                        ArrayList.class.getMethod("indexOf", Object.class));
60
61        mock = builder.createMock();
62
63        expect(mock.size()).andReturn(3);
64        expect(mock.contains("test")).andReturn(true);
65        expect(mock.add("added")).andReturn(true);
66        mock.clear();
67        expect(mock.isEmpty()).andReturn(false);
68        expect(mock.get(1)).andReturn("result");
69        expect(mock.indexOf("t")).andReturn(2);
70
71        replay(mock);
72
73        assertEquals(3, mock.size());
74        assertTrue(mock.contains("test"));
75        assertTrue(mock.add("added"));
76        mock.clear();
77        assertFalse(mock.isEmpty());
78        assertEquals("result", mock.get(1));
79        assertEquals(2, mock.indexOf("t"));
80
81        verify(mock);
82    }
83
84    @Test(expected = IllegalArgumentException.class)
85    public void testAddMethod_NotExisting() {
86        builder.addMockedMethod("..");
87    }
88
89    @Test(expected = IllegalArgumentException.class)
90    public void testAddMethodWithParams_NotExisting() {
91        builder.addMockedMethod("..", String.class);
92    }
93
94    private static final String errorMessage = "Method is not found, null, final, private or synthetic and so can't be mocked";
95
96    @Test
97    public void testAddMethod_Final() throws Exception {
98        MockBuilder<A> builder = new MockBuilder<>(A.class);
99        try {
100            builder.addMockedMethod(A.class.getMethod("foo", String.class));
101            fail("shouldn't be allowed to be mocked");
102        } catch (IllegalArgumentException e) {
103            assertEquals(errorMessage, e.getMessage());
104        }
105        try {
106            builder.addMockedMethod("foo");
107            fail("shouldn't be allowed to be mocked");
108        } catch (IllegalArgumentException e) {
109            assertEquals(errorMessage, e.getMessage());
110        }
111        try {
112            builder.addMockedMethod("foo", String.class);
113            fail("shouldn't be allowed to be mocked");
114        } catch (IllegalArgumentException e) {
115            assertEquals(errorMessage, e.getMessage());
116        }
117    }
118
119    @Test
120    public void testAddMethods_Final() throws Exception {
121        MockBuilder<A> builder = new MockBuilder<>(A.class);
122        try {
123            builder.addMockedMethods(A.class.getMethod("foo", String.class));
124            fail("shouldn't be allowed to be mocked");
125        } catch (IllegalArgumentException e) {
126            assertEquals(errorMessage, e.getMessage());
127        }
128        try {
129            builder.addMockedMethods("foo");
130            fail("shouldn't be allowed to be mocked");
131        } catch (IllegalArgumentException e) {
132            assertEquals(errorMessage, e.getMessage());
133        }
134    }
135
136    @Test
137    public void testWithConstructorParams() {
138        builder.withConstructor(int.class).withArgs(-3);
139        try {
140            builder.createMock();
141            fail("instantiation should fail because of negative");
142        } catch (RuntimeException e) {
143        }
144    }
145
146    @Test(expected = IllegalArgumentException.class)
147    public void testWithConstructor_WrongClass() {
148        builder.withConstructor(long.class);
149    }
150
151    @Test
152    public void testWithEmptyConstructor() {
153        EmptyConstructor instance = new MockBuilder<>(EmptyConstructor.class)
154                .withConstructor().createMock();
155        assertEquals("foo", instance.setByConstructor);
156    }
157
158    public static class EmptyConstructor {
159        private final String setByConstructor;
160
161        public EmptyConstructor() {
162            this.setByConstructor = "foo";
163        }
164    }
165
166    @Test
167    public void testWithEmptyConstructor_NoEmptyConstructor() {
168        try {
169            createMockBuilder(Integer.class).withConstructor().createMock();
170            fail("no empty constructor should be found");
171        } catch (IllegalArgumentException e) {
172        }
173    }
174
175    @Test
176    public void testWithConstructor() throws NoSuchMethodException {
177        builder.withConstructor(ArrayList.class.getConstructor(int.class)).withArgs(-3);
178        try {
179            builder.createMock();
180            fail("instantiation should fail because of negative");
181        } catch (RuntimeException e) {
182        }
183    }
184
185    @Test(expected = IllegalStateException.class)
186    public void testWithConstructor_Twice() {
187        builder.withConstructor(int.class).withConstructor(int.class);
188    }
189
190    @Test
191    public void testWithConstructorConstructorArgs() throws NoSuchMethodException {
192        ConstructorArgs args = new ConstructorArgs(ArrayList.class.getConstructor(int.class),
193            -3);
194        builder.withConstructor(args);
195        try {
196            builder.createMock();
197            fail("instantiation should fail because of negative");
198        } catch (RuntimeException e) {
199        }
200    }
201
202    @Test
203    public void testWithConstructorWithArgs() {
204        builder.withConstructor(-3);
205        try {
206            builder.createMock();
207            fail("instantiation should fail because of negative");
208        } catch (RuntimeException e) {
209        }
210    }
211
212    @Test(expected = IllegalArgumentException.class)
213    public void testWithConstructorWithArgs_NotExisting() {
214        builder.withConstructor("string");
215    }
216
217    @Test
218    public void testWithArgsTwice() {
219        try {
220            builder.withConstructor(int.class).withArgs(3).withArgs(2);
221            fail("withArgs called twice");
222        } catch (IllegalStateException e) {
223            assertEquals("Trying to define the constructor arguments more than once.", e.getMessage());
224        }
225    }
226
227    @Test
228    public void testWithArgs_WithoutConstructor() {
229        try {
230            builder.withArgs(2);
231            fail("withArgs without constructor");
232        } catch (IllegalStateException e) {
233            assertEquals("Trying to define constructor arguments without first setting their type.",
234                    e.getMessage());
235        }
236    }
237
238    @Test
239    public void testCreateMockIMocksControl() {
240        IMocksControl ctrl = createControl();
241        mock = builder.createMock(ctrl);
242        assertSame(MocksControl.getControl(mock), ctrl);
243    }
244
245    @Test
246    public void testCreateMock() {
247        mock = builder.addMockedMethod("size").addMockedMethod("toString").createMock();
248        replay(mock);
249        try {
250            mock.size();
251            fail("Unexpected call");
252        } catch (AssertionError e) {
253        }
254    }
255
256    @Test
257    public void testCreateNiceMock() {
258        mock = builder.addMockedMethod("size").addMockedMethod("toString").createNiceMock();
259        replay(mock);
260        assertEquals(0, mock.size());
261        verify(mock);
262    }
263
264    @Test
265    public void testCreateStrictMock() {
266        mock = builder.addMockedMethod("size").addMockedMethod("clear").addMockedMethod("toString")
267                .createStrictMock();
268        expect(mock.size()).andReturn(1);
269        mock.clear();
270        replay(mock);
271        try {
272            mock.clear();
273            fail("Unexpected call");
274        } catch (AssertionError e) {
275        }
276    }
277
278    @Test
279    public void testCreateMockStringIMocksControl() {
280        IMocksControl ctrl = createControl();
281        mock = builder.addMockedMethod("toString").createMock("myName", ctrl);
282        assertSame(MocksControl.getControl(mock), ctrl);
283        assertTrue(mock.toString().contains("myName"));
284    }
285
286    @Test
287    public void testCreateMockString() {
288        mock = builder.addMockedMethod("size").addMockedMethod("toString").createMock("myName");
289        replay(mock);
290        try {
291            mock.size();
292            fail("Unexpected call");
293        } catch (AssertionError e) {
294            assertTrue(e.getMessage().contains("myName"));
295        }
296    }
297
298    @Test
299    public void testCreateNiceMockString() {
300        mock = builder.addMockedMethod("size").addMockedMethod("toString").createNiceMock("myName");
301        replay(mock);
302        assertEquals(0, mock.size());
303        verify(mock);
304        assertTrue(mock.toString().contains("myName"));
305    }
306
307    @Test
308    public void testCreateStrictMockString() {
309        mock = builder.addMockedMethod("size").addMockedMethod("clear").addMockedMethod("toString")
310                .createStrictMock("myName");
311        expect(mock.size()).andReturn(1);
312        mock.clear();
313        replay(mock);
314        try {
315            mock.clear();
316            fail("Unexpected call");
317        } catch (AssertionError e) {
318            assertTrue(e.getMessage().contains("myName"));
319        }
320    }
321
322    @Test(expected = IllegalStateException.class)
323    public void testCreateMock_ConstructorWithoutArgs() {
324        builder.withConstructor(int.class).createMock();
325    }
326
327    @Test
328    public void testWithMockSupport() {
329        EasyMockSupport support = new EasyMockSupport();
330        MockBuilderTest a = support.createMockBuilder(MockBuilderTest.class).addMockedMethods("myMethod", "toString").createMock(MockType.NICE);
331        expect(a.myMethod(2)).andReturn(1);
332        support.replayAll();
333        assertEquals(1, a.myMethod(2));
334        assertEquals(0, a.myMethod(3));
335        support.verifyAll();
336        assertEquals("EasyMock for class org.easymock.tests2.MockBuilderTest", a.toString());
337    }
338
339    @Test
340    public void testWithMockSupportNamed() {
341        EasyMockSupport support = new EasyMockSupport();
342        MockBuilderTest a = support.createMockBuilder(MockBuilderTest.class).addMockedMethods("myMethod", "toString").createMock("foo", MockType.NICE);
343        expect(a.myMethod(2)).andReturn(1);
344        support.replayAll();
345        assertEquals(1, a.myMethod(2));
346        assertEquals(0, a.myMethod(3));
347        support.verifyAll();
348        assertEquals("foo", a.toString());
349    }
350
351    public int myMethod(int i) {
352        return i;
353    }
354}
355
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)