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

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

Run Easymock automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright (c) 2001-2007 OFFIS, Tammo Freese.
3 * This program is made available under the terms of the MIT License.
4 */
5package org.easymock.tests2;
6
7import static org.easymock.EasyMock.*;
8import static org.junit.Assert.*;
9
10import org.easymock.tests.IMethods;
11import org.junit.Before;
12import org.junit.Test;
13
14public class UsageTest {
15
16    IMethods mock;
17
18    @Before
19    public void setup() {
20        mock = createMock(IMethods.class);
21    }
22
23    @Test
24    public void exactCallCountByLastCall() {
25        expect(mock.oneArg(false)).andReturn("Test").andReturn("Test2");
26        replay(mock);
27
28        assertEquals("Test", mock.oneArg(false));
29        assertEquals("Test2", mock.oneArg(false));
30
31        boolean failed = false;
32        try {
33            mock.oneArg(false);
34        } catch (AssertionError expected) {
35            failed = true;
36        }
37        if (!failed)
38            fail("expected AssertionError");
39    }
40
41    @Test
42    public void openCallCountByLastCall() {
43        expect(mock.oneArg(false)).andReturn("Test").andReturn("Test2").atLeastOnce();
44
45        replay(mock);
46
47        assertEquals("Test", mock.oneArg(false));
48        assertEquals("Test2", mock.oneArg(false));
49        assertEquals("Test2", mock.oneArg(false));
50    }
51
52    @Test
53    public void exactCallCountByLastThrowable() {
54        expect(mock.oneArg(false)).andReturn("Test").andReturn("Test2").andThrow(new IndexOutOfBoundsException());
55
56        replay(mock);
57
58        assertEquals("Test", mock.oneArg(false));
59        assertEquals("Test2", mock.oneArg(false));
60
61        try {
62            mock.oneArg(false);
63            fail();
64        } catch (IndexOutOfBoundsException expected) {
65        }
66
67        boolean failed = true;
68        try {
69            mock.oneArg(false);
70            failed = false;
71        } catch (AssertionError expected) {
72        }
73        if (!failed)
74            fail("expected AssertionError");
75    }
76
77    @Test
78    public void openCallCountByLastThrowable() {
79        expect(mock.oneArg(false)).andReturn("Test").andReturn("Test2").andThrow(new IndexOutOfBoundsException())
80                .atLeastOnce();
81
82        replay(mock);
83
84        assertEquals("Test", mock.oneArg(false));
85        assertEquals("Test2", mock.oneArg(false));
86
87        try {
88            mock.oneArg(false);
89        } catch (IndexOutOfBoundsException expected) {
90        }
91        try {
92            mock.oneArg(false);
93        } catch (IndexOutOfBoundsException expected) {
94        }
95    }
96
97    @Test
98    public void moreThanOneArgument() {
99        expect(mock.threeArgumentMethod(1, "2", "3")).andReturn("Test").times(2);
100
101        replay(mock);
102
103        assertEquals("Test", mock.threeArgumentMethod(1, "2", "3"));
104
105        boolean failed = true;
106        try {
107            verify(mock);
108            failed = false;
109        } catch (AssertionError expected) {
110            assertEquals("\n  Expectation failure on verify:"
111                    + "\n    threeArgumentMethod(1, \"2\", \"3\"): expected: 2, actual: 1", expected.getMessage());
112        }
113        if (!failed) {
114            fail("exception expected");
115        }
116    }
117
118    @Test
119    public void wrongArguments() {
120        mock.simpleMethodWithArgument("3");
121        replay(mock);
122
123        try {
124            mock.simpleMethodWithArgument("5");
125            fail();
126        } catch (AssertionError expected) {
127            assertEquals("\n  Unexpected method call simpleMethodWithArgument(\"5\"):"
128                    + "\n    simpleMethodWithArgument(\"3\"): expected: 1, actual: 0", expected.getMessage());
129        }
130
131    }
132
133    @Test
134    public void summarizeSameObjectArguments() {
135        mock.simpleMethodWithArgument("3");
136        mock.simpleMethodWithArgument("3");
137        replay(mock);
138
139        try {
140            mock.simpleMethodWithArgument("5");
141            fail();
142        } catch (AssertionError expected) {
143            assertEquals("\n  Unexpected method call simpleMethodWithArgument(\"5\"):"
144                    + "\n    simpleMethodWithArgument(\"3\"): expected: 2, actual: 0", expected.getMessage());
145        }
146
147    }
148
149    @Test
150    public void argumentsOrdered() {
151        mock.simpleMethodWithArgument("4");
152        mock.simpleMethodWithArgument("3");
153        mock.simpleMethodWithArgument("2");
154        mock.simpleMethodWithArgument("0");
155        mock.simpleMethodWithArgument("1");
156        replay(mock);
157
158        try {
159            mock.simpleMethodWithArgument("5");
160            fail("exception expected");
161        } catch (AssertionError expected) {
162            assertEquals("\n  Unexpected method call simpleMethodWithArgument(\"5\"):"
163                    + "\n    simpleMethodWithArgument(\"4\"): expected: 1, actual: 0"
164                    + "\n    simpleMethodWithArgument(\"3\"): expected: 1, actual: 0"
165                    + "\n    simpleMethodWithArgument(\"2\"): expected: 1, actual: 0"
166                    + "\n    simpleMethodWithArgument(\"0\"): expected: 1, actual: 0"
167                    + "\n    simpleMethodWithArgument(\"1\"): expected: 1, actual: 0", expected.getMessage());
168        }
169
170    }
171
172    @Test
173    public void mixingOrderedAndUnordered() {
174        mock.simpleMethodWithArgument("2");
175        mock.simpleMethodWithArgument("1");
176        checkOrder(mock, true);
177        mock.simpleMethodWithArgument("3");
178        mock.simpleMethodWithArgument("4");
179        checkOrder(mock, false);
180        mock.simpleMethodWithArgument("6");
181        mock.simpleMethodWithArgument("7");
182        mock.simpleMethodWithArgument("5");
183
184        replay(mock);
185
186        mock.simpleMethodWithArgument("1");
187        mock.simpleMethodWithArgument("2");
188
189        boolean failed = false;
190        try {
191            mock.simpleMethodWithArgument("4");
192        } catch (AssertionError e) {
193            failed = true;
194        }
195        if (!failed) {
196            fail();
197        }
198
199        mock.simpleMethodWithArgument("3");
200        mock.simpleMethodWithArgument("4");
201        mock.simpleMethodWithArgument("5");
202        mock.simpleMethodWithArgument("6");
203        mock.simpleMethodWithArgument("7");
204
205        verify(mock);
206
207    }
208
209}
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

Trigger code on LambdaTest Cloud Grid

Execute automation tests with on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
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)