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

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

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.EasyMock;
19import org.easymock.LogicalOperator;
20import org.easymock.internal.matchers.Equals;
21import org.easymock.tests.IMethods;
22import org.junit.Before;
23import org.junit.Test;
24
25import java.math.BigDecimal;
26import java.util.Collections;
27import java.util.Comparator;
28import java.util.List;
29
30import static org.easymock.EasyMock.*;
31import static org.junit.Assert.*;
32
33/**
34 * @author OFFIS, Tammo Freese
35 */
36public class UsageConstraintsTest {
37    private IMethods mock;
38
39    @Before
40    public void setUp() {
41        mock = createMock(IMethods.class);
42    }
43
44    @Test
45    public void equalsMissing() {
46        mock.simpleMethodWithArgument(not(eq("asd")));
47        try {
48            mock.simpleMethodWithArgument(not("jkl"));
49            fail();
50        } catch (IllegalStateException e) {
51            assertEquals("no matchers found.", e.getMessage());
52        }
53        try {
54            mock.simpleMethodWithArgument(or(eq("jkl"), "asd"));
55            fail();
56        } catch (IllegalStateException e) {
57            assertEquals("2 matchers expected, 1 recorded.", e.getMessage());
58        }
59        try {
60            mock.threeArgumentMethod(1, "asd", eq("asd"));
61            fail();
62        } catch (IllegalStateException e) {
63            assertEquals(
64                    "3 matchers expected, 1 recorded.\n"
65                            + "This exception usually occurs when matchers are mixed with raw values when recording a method:\n"
66                            + "\tfoo(5, eq(6));\t// wrong\n"
67                            + "You need to use no matcher at all or a matcher for every single param:\n"
68                            + "\tfoo(eq(5), eq(6));\t// right\n" + "\tfoo(5, 6);\t// also right",
69                    e.getMessage());
70        }
71
72    }
73
74    @Test
75    public void differentConstraintsOnSameCall() {
76        mock.simpleMethodWithArgument(isNull());
77        mock.simpleMethodWithArgument(notNull());
78        replay(mock);
79        mock.simpleMethodWithArgument(null);
80        mock.simpleMethodWithArgument("2");
81    }
82
83    @SuppressWarnings("unlikely-arg-type")
84    @Test
85    public void equals() {
86        assertEquals(new Equals(null), new Equals(null));
87        assertEquals(new Equals(2), new Equals(2));
88        assertNotEquals(null, new Equals(null));
89        assertNotEquals("Test", new Equals(null));
90        try {
91            new Equals(null).hashCode();
92            fail();
93        } catch (UnsupportedOperationException expected) {
94        }
95    }
96
97    @Test
98    public void constraints() {
99        expect(mock.threeArgumentMethod(and(geq(7), leq(10)), isA(String.class), contains("123"))).andReturn(
100                "456").atLeastOnce();
101        replay(mock);
102        boolean failed = false;
103        try {
104            mock.threeArgumentMethod(11, "", "01234");
105        } catch (AssertionError expected) {
106            failed = true;
107        }
108        if (!failed) {
109            fail();
110        }
111        failed = false;
112        try {
113            mock.threeArgumentMethod(8, new Object(), "01234");
114        } catch (AssertionError expected) {
115            failed = true;
116        }
117        if (!failed) {
118            fail();
119        }
120        failed = false;
121        try {
122            mock.threeArgumentMethod(8, "", "no match");
123        } catch (AssertionError expected) {
124            failed = true;
125        }
126        if (!failed) {
127            fail();
128        }
129        assertEquals("456", mock.threeArgumentMethod(8, "", "01234"));
130        verifyRecording(mock);
131    }
132
133    @Test
134    public void andOverloaded() {
135        expect(mock.oneArg(and(eq(false), eq(false)))).andReturn("0");
136        expect(mock.oneArg(and(eq((byte) 1), eq((byte) 1)))).andReturn("1");
137        expect(mock.oneArg(and(eq('a'), eq('a')))).andReturn("2");
138        expect(mock.oneArg(and(eq((double) 1), eq((double) 1)))).andReturn("3");
139        expect(mock.oneArg(and(eq((float) 1), eq((float) 1)))).andReturn("4");
140        expect(mock.oneArg(and(eq(1), eq(1)))).andReturn("5");
141        expect(mock.oneArg(and(eq((long) 1), eq((long) 1)))).andReturn("6");
142        expect(mock.oneArg(and(eq((short) 1), eq((short) 1)))).andReturn("7");
143        expect(mock.oneArg(and(contains("a"), contains("d")))).andReturn("8");
144        expect(mock.oneArg(and(isA(Class.class), eq(Object.class)))).andReturn("9");
145        replay(mock);
146        assertEquals("9", mock.oneArg(Object.class));
147        assertEquals("0", mock.oneArg(false));
148        assertEquals("1", mock.oneArg((byte) 1));
149        assertEquals("2", mock.oneArg('a'));
150        assertEquals("3", mock.oneArg((double) 1));
151        assertEquals("7", mock.oneArg((short) 1));
152        assertEquals("8", mock.oneArg("abcde"));
153        assertEquals("4", mock.oneArg((float) 1));
154        assertEquals("5", mock.oneArg(1));
155        assertEquals("6", mock.oneArg((long) 1));
156        verify(mock);
157    }
158
159    @Test
160    public void orOverloaded() {
161        expect(mock.oneArg(or(eq(false), eq(true)))).andReturn("0");
162        expect(mock.oneArg(or(eq((byte) 1), eq((byte) 2)))).andReturn("1");
163        expect(mock.oneArg(or(eq((char) 1), eq((char) 2)))).andReturn("2");
164        expect(mock.oneArg(or(eq((double) 1), eq((double) 2)))).andReturn("3");
165        expect(mock.oneArg(or(eq((float) 1), eq((float) 2)))).andReturn("4");
166        expect(mock.oneArg(or(eq(1), eq(2)))).andReturn("5");
167        expect(mock.oneArg(or(eq((long) 1), eq((long) 2)))).andReturn("6");
168        expect(mock.oneArg(or(eq((short) 1), eq((short) 2)))).andReturn("7");
169        expect(mock.oneArg(or(eq("asd"), eq("jkl")))).andReturn("8");
170        expect(mock.oneArg(or(eq(this.getClass()), eq(Object.class)))).andReturn("9");
171        replay(mock);
172        assertEquals("9", mock.oneArg(Object.class));
173        assertEquals("0", mock.oneArg(true));
174        assertEquals("1", mock.oneArg((byte) 2));
175        assertEquals("2", mock.oneArg((char) 1));
176        assertEquals("3", mock.oneArg((double) 2));
177        assertEquals("7", mock.oneArg((short) 1));
178        assertEquals("8", mock.oneArg("jkl"));
179        assertEquals("4", mock.oneArg((float) 1));
180        assertEquals("5", mock.oneArg(2));
181        assertEquals("6", mock.oneArg((long) 1));
182        verify(mock);
183    }
184
185    @Test
186    public void notOverloaded() {
187        expect(mock.oneArg(not(eq(false)))).andReturn("0");
188        expect(mock.oneArg(not(eq((byte) 1)))).andReturn("1");
189        expect(mock.oneArg(not(eq('a')))).andReturn("2");
190        expect(mock.oneArg(not(eq((double) 1)))).andReturn("3");
191        expect(mock.oneArg(not(eq((float) 1)))).andReturn("4");
192        expect(mock.oneArg(not(eq(1)))).andReturn("5");
193        expect(mock.oneArg(not(eq((long) 1)))).andReturn("6");
194        expect(mock.oneArg(not(eq((short) 1)))).andReturn("7");
195        expect(mock.oneArg(not(contains("a")))).andReturn("8");
196        expect(mock.oneArg(not(isA(Class.class)))).andReturn("9");
197        replay(mock);
198        assertEquals("9", mock.oneArg(new Object()));
199        assertEquals("0", mock.oneArg(true));
200        assertEquals("1", mock.oneArg((byte) 2));
201        assertEquals("2", mock.oneArg('b'));
202        assertEquals("3", mock.oneArg((double) 2));
203        assertEquals("7", mock.oneArg((short) 2));
204        assertEquals("8", mock.oneArg("bcde"));
205        assertEquals("4", mock.oneArg((float) 2));
206        assertEquals("5", mock.oneArg(2));
207        assertEquals("6", mock.oneArg((long) 2));
208        verify(mock);
209    }
210
211    @Test
212    public void lessOrEqualOverloaded() {
213        expect(mock.oneArg(leq((byte) 1))).andReturn("1");
214        expect(mock.oneArg(leq((double) 1))).andReturn("3");
215        expect(mock.oneArg(leq((float) 1))).andReturn("4");
216        expect(mock.oneArg(leq(1))).andReturn("5");
217        expect(mock.oneArg(leq((long) 1))).andReturn("6");
218        expect(mock.oneArg(leq((short) 1))).andReturn("7");
219        expect(mock.oneArg(leq(new BigDecimal("1")))).andReturn("8");
220        replay(mock);
221        assertEquals("1", mock.oneArg((byte) 1));
222        assertEquals("3", mock.oneArg((double) 1));
223        assertEquals("7", mock.oneArg((short) 0));
224        assertEquals("4", mock.oneArg((float) -5));
225        assertEquals("5", mock.oneArg(-2));
226        assertEquals("6", mock.oneArg((long) -3));
227        assertEquals("8", mock.oneArg(new BigDecimal("0.5")));
228        verify(mock);
229    }
230
231    @Test
232    public void lessThanOverloaded() {
233        expect(mock.oneArg(lt((byte) 1))).andReturn("1");
234        expect(mock.oneArg(lt((double) 1))).andReturn("3");
235        expect(mock.oneArg(lt((float) 1))).andReturn("4");
236        expect(mock.oneArg(lt(1))).andReturn("5");
237        expect(mock.oneArg(lt((long) 1))).andReturn("6");
238        expect(mock.oneArg(lt((short) 1))).andReturn("7");
239        expect(mock.oneArg(lt(new BigDecimal("1")))).andReturn("8");
240        replay(mock);
241        assertEquals("1", mock.oneArg((byte) 0));
242        assertEquals("3", mock.oneArg((double) 0));
243        assertEquals("7", mock.oneArg((short) 0));
244        assertEquals("4", mock.oneArg((float) -4));
245        assertEquals("5", mock.oneArg(-34));
246        assertEquals("6", mock.oneArg((long) -6));
247        assertEquals("8", mock.oneArg(new BigDecimal("0.5")));
248        verify(mock);
249    }
250
251    @Test
252    public void greaterOrEqualOverloaded() {
253        expect(mock.oneArg(geq((byte) 1))).andReturn("1");
254        expect(mock.oneArg(geq((double) 1))).andReturn("3");
255        expect(mock.oneArg(geq((float) 1))).andReturn("4");
256        expect(mock.oneArg(geq(1))).andReturn("5");
257        expect(mock.oneArg(geq((long) 1))).andReturn("6");
258        expect(mock.oneArg(geq((short) 1))).andReturn("7");
259        expect(mock.oneArg(geq(new BigDecimal("1")))).andReturn("8");
260        replay(mock);
261        assertEquals("1", mock.oneArg((byte) 2));
262        assertEquals("3", mock.oneArg((double) 1));
263        assertEquals("7", mock.oneArg((short) 2));
264        assertEquals("4", mock.oneArg((float) 3));
265        assertEquals("5", mock.oneArg(4));
266        assertEquals("6", mock.oneArg((long) 5));
267        assertEquals("8", mock.oneArg(new BigDecimal("1.5")));
268        verify(mock);
269    }
270
271    @Test
272    public void greaterThanOverloaded() {
273        expect(mock.oneArg(gt((byte) 1))).andReturn("1");
274        expect(mock.oneArg(gt((double) 1))).andReturn("3");
275        expect(mock.oneArg(gt((float) 1))).andReturn("4");
276        expect(mock.oneArg(gt(1))).andReturn("5");
277        expect(mock.oneArg(gt((long) 1))).andReturn("6");
278        expect(mock.oneArg(gt((short) 1))).andReturn("7");
279        expect(mock.oneArg(gt(new BigDecimal("1")))).andReturn("8");
280        replay(mock);
281        assertEquals("1", mock.oneArg((byte) 2));
282        assertEquals("3", mock.oneArg((double) 2));
283        assertEquals("7", mock.oneArg((short) 2));
284        assertEquals("4", mock.oneArg((float) 3));
285        assertEquals("5", mock.oneArg(2));
286        assertEquals("6", mock.oneArg((long) 5));
287        assertEquals("8", mock.oneArg(new BigDecimal("1.5")));
288        verify(mock);
289    }
290
291    @Test
292    public void cmpTo() {
293        expect(mock.oneArg(cmpEq(new BigDecimal("1.5")))).andReturn("0");
294        replay(mock);
295        assertEquals("0", mock.oneArg(new BigDecimal("1.50")));
296        verify(mock);
297    }
298
299    public static class A {
300        private final int value;
301
302        public A(int value) {
303            this.value = value;
304        }
305
306        public int getValue() {
307            return value;
308        }
309    }
310
311    @Test
312    public void compareWithComparator() {
313        // Undertype just to make sure the generic typing works
314        Comparator<Object> comparator = new Comparator<Object>() {
315            private int compare(A a1, A a2) {
316                return a1.getValue() - a2.getValue();
317            }
318
319            @Override
320            public int compare(Object o1, Object o2) {
321                return compare((A) o1, (A) o2);
322            }
323        };
324
325        // Check my comparator works
326        assertTrue(comparator.compare(new A(1), new A(2)) < 0);
327        assertTrue(comparator.compare(new A(2), new A(1)) > 0);
328        assertEquals(0, comparator.compare(new A(1), new A(1)));
329
330        // Now test EasyMock.cmp
331        checkOrder(mock, true);
332
333        expect(mock.oneArg(cmp(new A(5), comparator, LogicalOperator.EQUAL))).andReturn("0");
334
335        expect(mock.oneArg(cmp(new A(5), comparator, LogicalOperator.GREATER))).andReturn("1");
336
337        expect(mock.oneArg(cmp(new A(5), comparator, LogicalOperator.GREATER_OR_EQUAL))).andReturn("2");
338        expect(mock.oneArg(cmp(new A(5), comparator, LogicalOperator.GREATER_OR_EQUAL))).andReturn("2");
339
340        expect(mock.oneArg(cmp(new A(5), comparator, LogicalOperator.LESS_OR_EQUAL))).andReturn("3");
341        expect(mock.oneArg(cmp(new A(5), comparator, LogicalOperator.LESS_OR_EQUAL))).andReturn("3");
342
343        expect(mock.oneArg(cmp(new A(5), comparator, LogicalOperator.LESS_THAN))).andReturn("4");
344
345        replay(mock);
346
347        checkItFails(null); // null is not comparable so always return false
348        try {
349            mock.oneArg("");
350            fail();
351        } catch (AssertionError e) {
352        } // different type isn't either
353
354        checkItFails(new A(4));
355        checkItFails(new A(6));
356        assertEquals("0", mock.oneArg(new A(5)));
357
358        checkItFails(new A(4));
359        checkItFails(new A(5));
360        assertEquals("1", mock.oneArg(new A(6)));
361
362        checkItFails(new A(4));
363        assertEquals("2", mock.oneArg(new A(6)));
364        assertEquals("2", mock.oneArg(new A(5)));
365
366        checkItFails(new A(6));
367        assertEquals("3", mock.oneArg(new A(4)));
368        assertEquals("3", mock.oneArg(new A(5)));
369
370        checkItFails(new A(5));
371        checkItFails(new A(6));
372        assertEquals("4", mock.oneArg(new A(4)));
373
374        verifyRecording(mock);
375    }
376
377    private void checkItFails(A a) {
378        try {
379            mock.oneArg(a);
380            fail();
381        } catch (AssertionError e) {
382        }
383    }
384
385    @Test
386    public void any() {
387        expect(mock.oneArg(anyBoolean())).andReturn("0");
388        expect(mock.oneArg(anyByte())).andReturn("1");
389        expect(mock.oneArg(anyChar())).andReturn("2");
390        expect(mock.oneArg(anyDouble())).andReturn("3");
391        expect(mock.oneArg(anyFloat())).andReturn("4");
392        expect(mock.oneArg(anyInt())).andReturn("5");
393        expect(mock.oneArg(anyLong())).andReturn("6");
394        expect(mock.oneArg(anyShort())).andReturn("7");
395        expect(mock.oneArg(anyObject())).andReturn("8");
396        expect(mock.oneArg(anyObject(String.class))).andReturn("9");
397        expect(mock.oneArg(EasyMock.<List<String>> anyObject())).andReturn("9"); // make sure there's no warning on the cast
398        expect(mock.oneArg(anyString())).andReturn("10");
399        replay(mock);
400        assertEquals("9", mock.oneArg(Collections.emptyList()));
401        assertEquals("0", mock.oneArg(true));
402        assertEquals("1", mock.oneArg((byte) 1));
403        assertEquals("2", mock.oneArg((char) 1));
404        assertEquals("3", mock.oneArg((double) 1));
405        assertEquals("7", mock.oneArg((short) 1));
406        assertEquals("8", mock.oneArg("Test"));
407        assertEquals("4", mock.oneArg((float) 1));
408        assertEquals("5", mock.oneArg(1));
409        assertEquals("6", mock.oneArg((long) 1));
410        assertEquals("9", mock.oneArg("Other Test"));
411        assertEquals("10", mock.oneArg(""));
412        verify(mock);
413    }
414
415    @Test
416    public void arrayEquals() {
417        expect(mock.oneArray(aryEq(new boolean[] { true }))).andReturn("0");
418        expect(mock.oneArray(aryEq(new byte[] { 1 }))).andReturn("1");
419        expect(mock.oneArray(aryEq(new char[] { 1 }))).andReturn("2");
420        expect(mock.oneArray(aryEq(new double[] { 1 }))).andReturn("3");
421        expect(mock.oneArray(aryEq(new float[] { 1 }))).andReturn("4");
422        expect(mock.oneArray(aryEq(new int[] { 1 }))).andReturn("5");
423        expect(mock.oneArray(aryEq(new long[] { 1 }))).andReturn("6");
424        expect(mock.oneArray(aryEq(new short[] { 1 }))).andReturn("7");
425        expect(mock.oneArray(aryEq(new String[] { "Test" }))).andReturn("8");
426        expect(mock.oneArray(aryEq(new Object[] { "Test" }))).andReturn("9");
427        replay(mock);
428        assertEquals("9", mock.oneArray(new Object[] { "Test" }));
429        assertEquals("0", mock.oneArray(new boolean[] { true }));
430        assertEquals("1", mock.oneArray(new byte[] { 1 }));
431        assertEquals("2", mock.oneArray(new char[] { 1 }));
432        assertEquals("3", mock.oneArray(new double[] { 1 }));
433        assertEquals("7", mock.oneArray(new short[] { 1 }));
434        assertEquals("8", mock.oneArray(new String[] { "Test" }));
435        assertEquals("4", mock.oneArray(new float[] { 1 }));
436        assertEquals("5", mock.oneArray(new int[] { 1 }));
437        assertEquals("6", mock.oneArray(new long[] { 1 }));
438        verify(mock);
439    }
440
441    @Test
442    public void greaterOrEqual() {
443        expect(mock.oneArg(geq(7))).andReturn("1").times(3);
444        expect(mock.oneArg(lt(7))).andStubReturn("2");
445
446        replay(mock);
447
448        assertEquals("1", mock.oneArg(7));
449        assertEquals("2", mock.oneArg(6));
450        assertEquals("1", mock.oneArg(8));
451        assertEquals("2", mock.oneArg(6));
452        assertEquals("1", mock.oneArg(9));
453
454        verify(mock);
455    }
456
457    @Test
458    public void greaterThan() {
459        expect(mock.oneArg(gt(7))).andReturn("1").times(3);
460        expect(mock.oneArg(leq(7))).andStubReturn("2");
461
462        replay(mock);
463
464        assertEquals("1", mock.oneArg(8));
465        assertEquals("2", mock.oneArg(7));
466        assertEquals("1", mock.oneArg(9));
467        assertEquals("2", mock.oneArg(6));
468        assertEquals("1", mock.oneArg(10));
469
470        verify(mock);
471    }
472
473    @Test
474    public void lessOrEqual() {
475        expect(mock.oneArg(leq(7))).andReturn("1").times(3);
476        expect(mock.oneArg(gt(7))).andStubReturn("2");
477
478        replay(mock);
479
480        assertEquals("1", mock.oneArg(7));
481        assertEquals("2", mock.oneArg(8));
482        assertEquals("1", mock.oneArg(6));
483        assertEquals("2", mock.oneArg(9));
484        assertEquals("1", mock.oneArg(5));
485
486        verify(mock);
487    }
488
489    @Test
490    public void lessThan() {
491        expect(mock.oneArg(lt(7))).andReturn("1").times(3);
492        expect(mock.oneArg(geq(7))).andStubReturn("2");
493
494        replay(mock);
495
496        assertEquals("1", mock.oneArg(5));
497        assertEquals("2", mock.oneArg(7));
498        assertEquals("1", mock.oneArg(6));
499        assertEquals("2", mock.oneArg(8));
500        assertEquals("1", mock.oneArg(4));
501
502        verify(mock);
503    }
504
505    @Test
506    public void testOr() {
507        expect(mock.oneArg(or(eq(7), eq(9)))).andReturn("1").atLeastOnce();
508        expect(mock.oneArg(anyInt())).andStubReturn("2");
509
510        replay(mock);
511
512        assertEquals("1", mock.oneArg(7));
513        assertEquals("1", mock.oneArg(9));
514        assertEquals("2", mock.oneArg(10));
515
516        verify(mock);
517    }
518
519    @Test
520    public void testNull() {
521        expect(mock.threeArgumentMethod(eq(1), isNull(), eq(""))).andReturn("1");
522        expect(mock.threeArgumentMethod(eq(1), isNull(Object.class), eq(""))).andReturn("2");
523        expect(mock.threeArgumentMethod(eq(1), not(isNull()), eq(""))).andStubReturn("3");
524
525        replay(mock);
526
527        assertEquals("1", mock.threeArgumentMethod(1, null, ""));
528        assertEquals("2", mock.threeArgumentMethod(1, null, ""));
529        assertEquals("3", mock.threeArgumentMethod(1, new Object(), ""));
530
531        verify(mock);
532    }
533
534    @Test
535    public void testNotNull() {
536        expect(mock.threeArgumentMethod(eq(1), notNull(), eq(""))).andReturn("1");
537        expect(mock.threeArgumentMethod(eq(1), notNull(Object.class), eq(""))).andReturn("2");
538        expect(mock.threeArgumentMethod(eq(1), not(notNull()), eq(""))).andStubReturn("3");
539
540        replay(mock);
541
542        assertEquals("1", mock.threeArgumentMethod(1, new Object(), ""));
543        assertEquals("2", mock.threeArgumentMethod(1, new Object(), ""));
544        assertEquals("3", mock.threeArgumentMethod(1, null, ""));
545
546        verify(mock);
547    }
548
549    @Test
550    public void testFind() {
551        expect(mock.oneArg(find("[a-z]+\\d"))).andReturn("1").atLeastOnce();
552        expect(mock.oneArg(find("\\d\\d"))).andStubReturn("2");
553
554        replay(mock);
555
556        assertEquals("1", mock.oneArg("1ab12"));
557        assertEquals("2", mock.oneArg("312xx"));
558
559        verify(mock);
560    }
561
562    @Test
563    public void testMatches() {
564        expect(mock.oneArg(matches("[a-z]+\\d\\d"))).andReturn("1").atLeastOnce();
565        expect(mock.oneArg(matches("\\d\\d\\d"))).andStubReturn("2");
566
567        replay(mock);
568
569        assertEquals("1", mock.oneArg("a12"));
570        assertEquals("2", mock.oneArg("131"));
571
572        verify(mock);
573    }
574
575    @Test
576    public void testContains() {
577        expect(mock.oneArg(contains("ab"))).andReturn("1").atLeastOnce();
578        expect(mock.oneArg(contains("bc"))).andStubReturn("2");
579
580        replay(mock);
581
582        assertEquals("1", mock.oneArg("xabcx"));
583        assertEquals("2", mock.oneArg("xdbcx"));
584
585        verify(mock);
586    }
587
588    @Test
589    public void testStartsWith() {
590        expect(mock.oneArg(startsWith("ab"))).andReturn("1").atLeastOnce();
591        expect(mock.oneArg(startsWith("bc"))).andStubReturn("2");
592
593        replay(mock);
594
595        assertEquals("1", mock.oneArg("abcx"));
596        assertEquals("2", mock.oneArg("bcxe"));
597
598        verify(mock);
599    }
600
601    @Test
602    public void testEndsWith() {
603        expect(mock.oneArg(endsWith("ab"))).andReturn("1").atLeastOnce();
604        expect(mock.oneArg(endsWith("bc"))).andStubReturn("2");
605
606        replay(mock);
607
608        assertEquals("1", mock.oneArg("xab"));
609        assertEquals("2", mock.oneArg("xbc"));
610
611        verify(mock);
612    }
613
614    @Test
615    public void equalsWithDelta() {
616        expect(mock.oneArg(eq(1.0D, 0.1D))).andReturn("1").atLeastOnce();
617        expect(mock.oneArg(eq(2.0D, 0.1D))).andStubReturn("2");
618        expect(mock.oneArg(eq(1.0F, 0.1F))).andReturn("3").atLeastOnce();
619        expect(mock.oneArg(eq(2.0F, 0.1F))).andStubReturn("4");
620        expect(mock.oneArg(eq(2.0F, 0.1F))).andStubReturn("4");
621
622        replay(mock);
623
624        assertEquals("1", mock.oneArg(1.0));
625        assertEquals("1", mock.oneArg(0.91));
626        assertEquals("1", mock.oneArg(1.09));
627        assertEquals("2", mock.oneArg(2.0));
628
629        assertEquals("3", mock.oneArg(1.0F));
630        assertEquals("3", mock.oneArg(0.91F));
631        assertEquals("3", mock.oneArg(1.09F));
632        assertEquals("4", mock.oneArg(2.0F));
633
634        verify(mock);
635    }
636
637    @SuppressWarnings("RedundantStringConstructorCall")
638    @Test
639    public void testSame() {
640        Object one = new String("1243");
641        Object two = new String("1243");
642
643        assertNotSame(one, two);
644        assertEquals(one, two);
645
646        expect(mock.oneArg(same(one))).andReturn("1").atLeastOnce();
647        expect(mock.oneArg(same(two))).andStubReturn("2");
648
649        replay(mock);
650
651        assertEquals("1", mock.oneArg(one));
652        assertEquals("2", mock.oneArg(two));
653
654        verify(mock);
655    }
656}
657
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)