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-2013 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 java.util.ArrayList;
19
20import org.easymock.ConstructorArgs;
21import org.easymock.EasyMockSupport;
22import org.easymock.IMocksControl;
23import org.easymock.MockType;
24import org.easymock.internal.MockBuilder;
25import org.easymock.internal.MocksControl;
26import org.junit.Before;
27import org.junit.Test;
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(final 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() throws Exception {
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        assertEquals(true, mock.contains("test"));
75        assertEquals(true, mock.add("added"));
76        mock.clear();
77        assertEquals(false, 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    @Test
95    public void testAddMethod_Final() throws Exception {
96        final String errorMessage = "Final methods can't be mocked";
97        final MockBuilder<A> builder = new MockBuilder<A>(A.class);
98        try {
99            builder.addMockedMethod(A.class.getMethod("foo", String.class));
100            fail("sholdn't be allowed to be mocked");
101        } catch (final IllegalArgumentException e) {
102            assertEquals(errorMessage, e.getMessage());
103        }
104        try {
105            builder.addMockedMethod("foo");
106            fail("sholdn't be allowed to be mocked");
107        } catch (final IllegalArgumentException e) {
108            assertEquals(errorMessage, e.getMessage());
109        }
110        try {
111            builder.addMockedMethod("foo", String.class);
112            fail("sholdn't be allowed to be mocked");
113        } catch (final IllegalArgumentException e) {
114            assertEquals(errorMessage, e.getMessage());
115        }
116    }
117
118    @Test
119    public void testAddMethods_Final() throws Exception {
120        final String errorMessage = "Final methods can't be mocked";
121        final MockBuilder<A> builder = new MockBuilder<A>(A.class);
122        try {
123            builder.addMockedMethods(A.class.getMethod("foo", String.class));
124            fail("sholdn't be allowed to be mocked");
125        } catch (final IllegalArgumentException e) {
126            assertEquals(errorMessage, e.getMessage());
127        }
128        try {
129            builder.addMockedMethods("foo");
130            fail("sholdn't be allowed to be mocked");
131        } catch (final 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 (final 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() throws Exception {
153        final EmptyConstructor instance = new MockBuilder<EmptyConstructor>(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() throws Exception {
168        try {
169            createMockBuilder(Integer.class).withConstructor().createMock();
170            fail("no empty constructor should be found");
171        } catch (final 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 (final 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        final ConstructorArgs args = new ConstructorArgs(ArrayList.class.getConstructor(int.class),
193                Integer.valueOf(-3));
194        builder.withConstructor(args);
195        try {
196            builder.createMock();
197            fail("instantiation should fail because of negative");
198        } catch (final RuntimeException e) {
199        }
200    }
201
202    @Test
203    public void testWithConstructorWithArgs() throws NoSuchMethodException {
204        builder.withConstructor(-3);
205        try {
206            builder.createMock();
207            fail("instantiation should fail because of negative");
208        } catch (final RuntimeException e) {
209        }
210    }
211
212    @Test(expected = IllegalArgumentException.class)
213    public void testWithConstructorWithArgs_NotExisting() throws NoSuchMethodException {
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 (final 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 (final 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        final 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 (final 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 (final AssertionError e) {
275        }
276    }
277
278    @Test
279    public void testCreateMockStringIMocksControl() {
280        final 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 (final 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() throws Throwable {
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 (final 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(a.myMethod(2), 1);
334        assertEquals(a.myMethod(3), 0);
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(a.myMethod(2), 1);
346        assertEquals(a.myMethod(3), 0);
347        support.verifyAll();
348        assertEquals("foo", a.toString());
349    }
350
351    public int myMethod(int i) {
352        return i;
353    }
354
355}
356
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)