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

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

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 static org.easymock.EasyMock.*;
19import static org.junit.Assert.*;
20
21import java.util.Arrays;
22
23import org.easymock.Capture;
24import org.easymock.CaptureType;
25import org.easymock.tests.IMethods;
26import org.junit.After;
27import org.junit.Before;
28import org.junit.Test;
29
30/**
31 * @author Henri Tremblay
32 */
33public class CaptureTest {
34
35    public static class A {
36        public String foo(IMethods methods) {
37            return methods.oneArg(2);
38        }
39    }
40
41    @Before
42    public void setUp() {
43    }
44
45    @After
46    public void tearDown() {
47    }
48
49    private Capture<Integer> testCaptureType(CaptureType type) {
50        IMethods mock = createMock(IMethods.class);
51        Capture<Integer> captured = Capture.newInstance(type);
52
53        expect(mock.oneArg(captureInt(captured))).andReturn("1");
54        expect(mock.oneArg(anyInt())).andReturn("1");
55        expect(mock.oneArg(captureInt(captured))).andReturn("2").times(2);
56        mock.twoArgumentMethod(captureInt(captured), eq(5));
57        mock.twoArgumentMethod(captureInt(captured), captureInt(captured));
58
59        replay(mock);
60
61        mock.oneArg(0);
62        mock.oneArg(1);
63        mock.oneArg(2);
64        mock.oneArg(3);
65        mock.twoArgumentMethod(4, 5);
66        mock.twoArgumentMethod(6, 7);
67
68        verify(mock);
69
70        return captured;
71    }
72
73    @Test
74    public void testCaptureFirst() {
75        Capture<Integer> captured = testCaptureType(CaptureType.FIRST);
76        assertEquals(0, (int) captured.getValue());
77    }
78
79    @Test
80    public void testCaptureLast() {
81        Capture<Integer> captured = testCaptureType(CaptureType.LAST);
82        assertEquals(7, (int) captured.getValue());
83    }
84
85    @Test
86    public void testCaptureAll() {
87        Capture<Integer> captured = testCaptureType(CaptureType.ALL);
88        assertEquals(Arrays.asList(0, 2, 3, 4, 6, 7), captured.getValues());
89    }
90
91    @Test
92    public void testCaptureNone() {
93        Capture<Integer> captured = testCaptureType(CaptureType.NONE);
94        assertFalse(captured.hasCaptured());
95    }
96
97    // capture in thread
98    // after replay issue?
99
100    @Test
101    public void testCaptureRightOne() {
102        Capture<String> captured = Capture.newInstance();
103        IMethods mock = createMock(IMethods.class);
104
105        expect(mock.oneArg(and(eq("test"), capture(captured)))).andReturn("answer1");
106        expect(mock.oneArg("a")).andReturn("answer2");
107
108        replay(mock);
109
110        assertEquals("answer2", mock.oneArg("a"));
111        assertFalse(captured.hasCaptured());
112
113        assertEquals("answer1", mock.oneArg("test"));
114        assertEquals("test", captured.getValue());
115
116        verify(mock);
117    }
118
119    @Test
120    public void testPrimitiveVsObject() {
121        Capture<Integer> capture = Capture.newInstance();
122        IMethods mock = createMock(IMethods.class);
123
124        expect(mock.oneArg(captureInt(capture))).andReturn("answer");
125        expect(mock.oneArg(capture(capture))).andReturn("answer");
126
127        replay(mock);
128
129        assertEquals("answer", mock.oneArg(2));
130        assertEquals(2, capture.getValue().intValue());
131
132        assertEquals("answer", mock.oneArg(Integer.valueOf(3)));
133        assertEquals(3, capture.getValue().intValue());
134
135        verify(mock);
136    }
137
138    @Test
139    public void testAnd() {
140        Capture<String> captured = Capture.newInstance();
141        IMethods mock = createMock(IMethods.class);
142
143        expect(mock.oneArg(and(capture(captured), eq("test")))).andReturn("answer");
144
145        replay(mock);
146
147        assertEquals("answer", mock.oneArg("test"));
148        assertEquals("test", captured.getValue());
149
150        verify(mock);
151    }
152
153    @Test
154    public void testPrimitive() {
155        Capture<Integer> captureI = Capture.newInstance();
156        Capture<Long> captureL = Capture.newInstance();
157        Capture<Float> captureF = Capture.newInstance();
158        Capture<Double> captureD = Capture.newInstance();
159        Capture<Byte> captureB = Capture.newInstance();
160        Capture<Character> captureC = Capture.newInstance();
161        Capture<Boolean> captureBool = Capture.newInstance();
162
163        IMethods mock = createMock(IMethods.class);
164
165        expect(mock.oneArg(captureInt(captureI))).andReturn("answerI");
166        expect(mock.oneArg(captureLong(captureL))).andReturn("answerL");
167        expect(mock.oneArg(captureFloat(captureF))).andReturn("answerF");
168        expect(mock.oneArg(captureDouble(captureD))).andReturn("answerD");
169        expect(mock.oneArg(captureByte(captureB))).andReturn("answerB");
170        expect(mock.oneArg(captureChar(captureC))).andReturn("answerC");
171        expect(mock.oneArg(captureBoolean(captureBool))).andReturn("answerZ");
172
173        replay(mock);
174
175        assertEquals("answerI", mock.oneArg(1));
176        assertEquals("answerL", mock.oneArg(2L));
177        assertEquals("answerF", mock.oneArg(3.0f));
178        assertEquals("answerD", mock.oneArg(4.0));
179        assertEquals("answerB", mock.oneArg((byte) 5));
180        assertEquals("answerC", mock.oneArg((char) 6));
181        assertEquals("answerZ", mock.oneArg(true));
182
183        assertEquals(1, captureI.getValue().intValue());
184        assertEquals(2L, captureL.getValue().longValue());
185        assertEquals(3.0f, captureF.getValue(), 0.0);
186        assertEquals(4.0, captureD.getValue(), 0.0);
187        assertEquals((byte) 5, captureB.getValue().byteValue());
188        assertEquals((char) 6, captureC.getValue().charValue());
189        assertTrue(captureBool.getValue());
190
191        verify(mock);
192    }
193
194    @Test
195    public void testCapture() {
196        Capture<String> capture = Capture.newInstance();
197        assertFalse(capture.hasCaptured());
198        try {
199            capture.getValue();
200            fail("Should not be allowed");
201        } catch (AssertionError e) {
202            assertEquals("Nothing captured yet", e.getMessage());
203        }
204        assertEquals("Nothing captured yet", capture.toString());
205        capture.setValue("s");
206        assertTrue(capture.hasCaptured());
207        assertEquals("s", capture.getValue());
208        assertEquals("s", capture.toString());
209        capture.reset();
210        assertFalse(capture.hasCaptured());
211        try {
212            capture.getValue();
213            fail();
214        } catch (AssertionError e) {
215            assertEquals("Nothing captured yet", e.getMessage());
216        }
217
218        capture.setValue(null);
219        assertTrue(capture.hasCaptured());
220        assertNull(capture.getValue());
221        assertEquals("null", capture.toString());
222    }
223
224    @Test
225    public void testCaptureMultiple() {
226        Capture<String> capture = Capture.newInstance(CaptureType.ALL);
227        capture.setValue("a");
228        capture.setValue("b");
229        try {
230            capture.getValue();
231            fail();
232        } catch (AssertionError e) {
233            assertEquals("More than one value captured: " + capture.getValues(), e.getMessage());
234        }
235        assertEquals(Arrays.asList("a", "b"), capture.getValues());
236    }
237
238    @Test
239    public void testCapture_2617107() {
240
241        IMethods mock = createMock(IMethods.class);
242
243        Capture<String> cap1 = Capture.newInstance();
244        Capture<String> cap2 = Capture.newInstance();
245        Capture<String> cap3 = Capture.newInstance();
246        Capture<String> cap4 = Capture.newInstance();
247
248        mock.simpleMethodWithArgument(and(isA(String.class), capture(cap1)));
249        mock.simpleMethodWithArgument(and(isA(String.class), capture(cap2)));
250        mock.simpleMethodWithArgument(and(isA(String.class), capture(cap3)));
251        mock.simpleMethodWithArgument(and(isA(String.class), capture(cap4)));
252
253        replay(mock);
254
255        String[] s = { "one", "two", "three", "four" };
256
257        for (String element : s) {
258            mock.simpleMethodWithArgument(element);
259        }
260
261        assertEquals("one", cap1.getValue());
262        assertEquals("two", cap2.getValue());
263        assertEquals("three", cap3.getValue());
264        assertEquals("four", cap4.getValue());
265
266        verify(mock);
267    }
268
269    @Test
270    public void testCaptureNonStrictControl_2133741() {
271        testCaptureHelper(createMock(IMethods.class));
272    }
273
274    @Test
275    public void testCaptureStrictControl_2133741() {
276        testCaptureHelper(createStrictMock(IMethods.class));
277    }
278
279    protected void testCaptureHelper(IMethods mock) {
280        Capture<String> capture1 = Capture.newInstance();
281        Capture<String> capture2 = Capture.newInstance();
282
283        mock.simpleMethodWithArgument(capture(capture1));
284        mock.simpleMethodWithArgument(capture(capture2));
285
286        replay(mock);
287        mock.simpleMethodWithArgument("a");
288        mock.simpleMethodWithArgument("b");
289        verify(mock);
290
291        assertTrue(capture1.hasCaptured());
292        assertTrue(capture2.hasCaptured());
293        assertNotSame(capture1.getValue(), capture2.getValue());
294    }
295
296    @Test
297    public void testCapture1_2446744() {
298        Capture<String> capture1 = Capture.newInstance();
299        Capture<String> capture2 = Capture.newInstance();
300        Capture<String> capture3 = Capture.newInstance();
301        IMethods mock = createMock(IMethods.class);
302        expect(mock.oneArg(capture(capture1))).andReturn("1").once();
303        expect(mock.oneArg(capture(capture2))).andReturn("2").once();
304        expect(mock.oneArg(capture(capture3))).andReturn("3").once();
305
306        replay(mock);
307
308        for (int i = 0; i < 3; i++) {
309            String string = "Run" + (i + 1);
310            mock.oneArg(string);
311        }
312
313        assertEquals("Run3", capture3.getValue());
314        assertEquals("Run2", capture2.getValue());
315        assertEquals("Run1", capture1.getValue());
316    }
317
318    @Test
319    public void testCapture2_2446744() {
320        Capture<String> capture = Capture.newInstance(CaptureType.ALL);
321        IMethods mock = createMock(IMethods.class);
322        expect(mock.oneArg(capture(capture))).andReturn("1").once();
323        expect(mock.oneArg(capture(capture))).andReturn("2").once();
324        expect(mock.oneArg(capture(capture))).andReturn("3").once();
325
326        replay(mock);
327
328        for (int i = 0; i < 3; i++) {
329            String string = "Run" + (i + 1);
330            mock.oneArg(string);
331        }
332
333        assertEquals(Arrays.asList("Run1", "Run2", "Run3"), capture.getValues());
334    }
335
336    @Test
337    public void testCaptureFromStub() {
338        Capture<String> capture = Capture.newInstance(CaptureType.ALL);
339        IMethods mock = createMock(IMethods.class);
340        expect(mock.oneArg(capture(capture))).andStubReturn("1");
341
342        replay(mock);
343
344        mock.oneArg("test");
345
346        assertEquals("test", capture.getValue());
347    }
348
349    @Test
350    public void testNewInstanceForcingType() {
351        // Just to test, we put it in a base class
352        Capture<? extends Number> capture = newCapture();
353
354        IMethods mock = createMock(IMethods.class);
355        expect(mock.oneArg(capture(capture))).andStubReturn("1");
356
357        replay(mock);
358
359        mock.oneArg(Long.valueOf(0)); // note that we can capture something else than an Integer
360        mock.oneArg(Long.valueOf(1));
361
362        assertEquals(1L, capture.getValue());
363    }
364
365    @Test
366    public void testNewInstanceWithCaptureTypeForcingType() {
367        // Just to test, we put it in a base class
368        Capture<? extends Number> capture = newCapture(CaptureType.FIRST);
369
370        IMethods mock = createMock(IMethods.class);
371        expect(mock.oneArg(capture(capture))).andStubReturn("1");
372
373        replay(mock);
374
375        mock.oneArg(Long.valueOf(0)); // note that we can capture something else than an Integer
376        mock.oneArg(Long.valueOf(1));
377
378        assertEquals(0L, capture.getValue());
379    }
380}
381
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)