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-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 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(final IMethods methods) {
37            return methods.oneArg(2);
38        }
39    }
40
41    @Before
42    public void setUp() throws Exception {
43    }
44
45    @After
46    public void tearDown() throws Exception {
47    }
48
49    private Capture<Integer> testCaptureType(final CaptureType type) {
50        final IMethods mock = createMock(IMethods.class);
51        final Capture<Integer> captured = new Capture<Integer>(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        final Capture<Integer> captured = testCaptureType(CaptureType.FIRST);
76        assertEquals(0, (int) captured.getValue());
77    }
78
79    @Test
80    public void testCaptureLast() {
81        final Capture<Integer> captured = testCaptureType(CaptureType.LAST);
82        assertEquals(7, (int) captured.getValue());
83    }
84
85    @Test
86    public void testCaptureAll() {
87        final 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        final 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        final Capture<String> captured = new Capture<String>();
103        final 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        final Capture<Integer> capture = new Capture<Integer>();
122        final 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        final Capture<String> captured = new Capture<String>();
141        final 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        final Capture<Integer> captureI = new Capture<Integer>();
156        final Capture<Long> captureL = new Capture<Long>();
157        final Capture<Float> captureF = new Capture<Float>();
158        final Capture<Double> captureD = new Capture<Double>();
159        final Capture<Byte> captureB = new Capture<Byte>();
160        final Capture<Character> captureC = new Capture<Character>();
161        final Capture<Boolean> captureBool = new Capture<Boolean>();
162
163        final 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().floatValue(), 0.0);
186        assertEquals(4.0, captureD.getValue().doubleValue(), 0.0);
187        assertEquals((byte) 5, captureB.getValue().byteValue());
188        assertEquals((char) 6, captureC.getValue().charValue());
189        assertEquals(true, captureBool.getValue().booleanValue());
190
191        verify(mock);
192    }
193
194    @Test
195    public void testCapture() {
196        final Capture<String> capture = new Capture<String>();
197        assertFalse(capture.hasCaptured());
198        try {
199            capture.getValue();
200            fail("Should not be allowed");
201        } catch (final 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 (final 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        final Capture<String> capture = new Capture<String>(CaptureType.ALL);
227        capture.setValue("a");
228        capture.setValue("b");
229        try {
230            capture.getValue();
231            fail();
232        } catch (final 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        final IMethods mock = createMock(IMethods.class);
242
243        final Capture<String> cap1 = new Capture<String>();
244        final Capture<String> cap2 = new Capture<String>();
245        final Capture<String> cap3 = new Capture<String>();
246        final Capture<String> cap4 = new Capture<String>();
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        final String[] s = { "one", "two", "three", "four" };
256
257        for (final 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(final IMethods mock) {
280        final Capture<String> capture1 = new Capture<String>();
281        final Capture<String> capture2 = new Capture<String>();
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        assertFalse(capture1.getValue() == capture2.getValue());
294    }
295
296    @Test
297    public void testCapture1_2446744() {
298        final Capture<String> capture1 = new Capture<String>();
299        final Capture<String> capture2 = new Capture<String>();
300        final Capture<String> capture3 = new Capture<String>();
301        final 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            final 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        final Capture<String> capture = new Capture<String>(CaptureType.ALL);
321        final 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            final 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        final Capture<String> capture = new Capture<String>(CaptureType.ALL);
339        final 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
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)