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

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

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