How to use method of org.easymock.internal.ReplayState class

Best Easymock code snippet using org.easymock.internal.ReplayState.

Run Easymock automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright 2001-2021 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.internal;
17
18import org.easymock.IAnswer;
19
20import java.io.Serializable;
21import java.util.concurrent.locks.ReentrantLock;
22
23/**
24 * @author OFFIS, Tammo Freese
25 */
26public class ReplayState implements IMocksControlState, Serializable {
27
28    private static final long serialVersionUID = 6314142602251047572L;
29
30    private final IMocksBehavior behavior;
31
32    private final ReentrantLock lock = new ReentrantLock();
33
34    public ReplayState(IMocksBehavior behavior) {
35        this.behavior = behavior;
36    }
37
38    @Override
39    public Object invoke(Invocation invocation) throws Throwable {
40
41        behavior.checkThreadSafety();
42
43        if (behavior.isThreadSafe()) {
44            // If thread safe, synchronize the mock
45            lock.lock();
46            try {
47                return invokeInner(invocation);
48            } finally {
49                lock.unlock();
50            }
51        }
52
53        return invokeInner(invocation);
54    }
55
56    private Object invokeInner(Invocation invocation) throws Throwable {
57        LastControl.pushCurrentInvocation(invocation);
58        try {
59            Result result = behavior.addActual(invocation);
60            try {
61                return result.answer();
62            } catch (Throwable t) {
63                if (result.shouldFillInStackTrace()) {
64                    throw new ThrowableWrapper(t);
65                }
66                throw t;
67            }
68        } finally {
69            LastControl.popCurrentInvocation();
70        }
71    }
72
73    @Override
74    public void verifyRecording() {
75        behavior.verifyRecording();
76    }
77
78    @Override
79    public void verifyUnexpectedCalls() {
80        behavior.verifyUnexpectedCalls();
81    }
82
83    @Override
84    public void verify() {
85        behavior.verify();
86    }
87
88    @Override
89    public void replay() {
90        throwWrappedIllegalStateException();
91    }
92
93    public void callback(Runnable runnable) {
94        throwWrappedIllegalStateException();
95    }
96
97    @Override
98    public void checkOrder(boolean value) {
99        throwWrappedIllegalStateException();
100    }
101
102    @Override
103    public void makeThreadSafe(boolean threadSafe) {
104        throwWrappedIllegalStateException();
105    }
106
107    @Override
108    public void checkIsUsedInOneThread(boolean shouldBeUsedInOneThread) {
109        throwWrappedIllegalStateException();
110    }
111
112    @Override
113    public void andReturn(Object value) {
114        throwWrappedIllegalStateException();
115    }
116
117    @Override
118    public void andThrow(Throwable throwable) {
119        throwWrappedIllegalStateException();
120    }
121
122    @Override
123    public void andAnswer(IAnswer<?> answer) {
124        throwWrappedIllegalStateException();
125    }
126
127    @Override
128    public void andDelegateTo(Object answer) {
129        throwWrappedIllegalStateException();
130    }
131
132    @Override
133    public void andVoid() {
134        throwWrappedIllegalStateException();
135    }
136
137    @Override
138    public void andStubReturn(Object value) {
139        throwWrappedIllegalStateException();
140    }
141
142    @Override
143    public void andStubThrow(Throwable throwable) {
144        throwWrappedIllegalStateException();
145    }
146
147    @Override
148    public void andStubAnswer(IAnswer<?> answer) {
149        throwWrappedIllegalStateException();
150    }
151
152    @Override
153    public void andStubDelegateTo(Object delegateTo) {
154        throwWrappedIllegalStateException();
155    }
156
157    @Override
158    public void asStub() {
159        throwWrappedIllegalStateException();
160    }
161
162    @Override
163    public void times(Range range) {
164        throwWrappedIllegalStateException();
165    }
166
167    private void throwWrappedIllegalStateException() {
168        throw new RuntimeExceptionWrapper(new IllegalStateException(
169                "This method must not be called in replay state."));
170    }
171
172    @Override
173    public void assertRecordState() {
174        throwWrappedIllegalStateException();
175    }
176}
177
Full Screen
copy
1/*
2 * Copyright 2001-2021 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 org.easymock.internal.ReplayState;
19import org.junit.Before;
20import org.junit.Test;
21
22import java.io.IOException;
23
24import static org.easymock.EasyMock.*;
25import static org.junit.Assert.*;
26
27/**
28 * @author OFFIS, Tammo Freese
29 */
30public class UsageVerifyTest {
31
32    private IMethods mock;
33
34    @Before
35    public void setup() {
36        mock = createMock(IMethods.class);
37    }
38
39    @Test
40    public void twoReturns() {
41        expect(mock.throwsNothing(true)).andReturn("Test").andReturn("Test2");
42
43        replay(mock);
44
45        assertEquals("Test", mock.throwsNothing(true));
46
47        boolean failed = true;
48
49        try {
50            verify(mock);
51            failed = false;
52        } catch (AssertionError expected) {
53            assertEquals("\n  Expectation failure on verify:"
54                    + "\n    IMethods.throwsNothing(true): expected: 2, actual: 1", expected.getMessage());
55            assertEquals("stack trace must be filled in", Util.getStackTrace(expected).indexOf(
56                ReplayState.class.getName()), -1);
57        }
58
59        if (!failed)
60            fail("AssertionError expected");
61
62        assertEquals("Test2", mock.throwsNothing(true));
63
64        verify(mock);
65
66        try {
67            mock.throwsNothing(true);
68            fail("AssertionError expected");
69        } catch (AssertionError expected) {
70            assertEquals("\n  Unexpected method call IMethods.throwsNothing(true):"
71                    + "\n    IMethods.throwsNothing(true): expected: 2, actual: 3", expected.getMessage());
72        }
73    }
74
75    @Test
76    public void atLeastTwoReturns() {
77        expect(mock.throwsNothing(true)).andReturn("Test").andReturn("Test2").atLeastOnce();
78
79        replay(mock);
80
81        assertEquals("Test", mock.throwsNothing(true));
82
83        try {
84            verify(mock);
85            fail("AssertionError expected");
86        } catch (AssertionError expected) {
87
88            assertEquals("\n  Expectation failure on verify:"
89                    + "\n    IMethods.throwsNothing(true): expected: at least 2, actual: 1", expected
90                    .getMessage());
91        }
92
93        assertEquals("Test2", mock.throwsNothing(true));
94        assertEquals("Test2", mock.throwsNothing(true));
95
96        verify(mock);
97    }
98
99    @Test
100    public void twoThrows() throws IOException {
101        expect(mock.throwsIOException(0)).andThrow(new IOException()).andThrow(new IOException());
102        expect(mock.throwsIOException(1)).andThrow(new IOException());
103
104        replay(mock);
105
106        try {
107            mock.throwsIOException(0);
108            fail("IOException expected");
109        } catch (IOException expected) {
110        }
111
112        try {
113            verify(mock);
114            fail("AssertionError expected");
115        } catch (AssertionError expected) {
116            assertEquals("\n  Expectation failure on verify:"
117                    + "\n    IMethods.throwsIOException(0 (int)): expected: 2, actual: 1"
118                    + "\n    IMethods.throwsIOException(1 (int)): expected: 1, actual: 0", expected.getMessage());
119        }
120
121        try {
122            mock.throwsIOException(0);
123            fail("IOException expected");
124        } catch (IOException expected) {
125        }
126
127        try {
128            verify(mock);
129            fail("AssertionError expected");
130        } catch (AssertionError expected) {
131            assertEquals("\n  Expectation failure on verify:"
132                    + "\n    IMethods.throwsIOException(1 (int)): expected: 1, actual: 0", expected.getMessage());
133        }
134
135        try {
136            mock.throwsIOException(1);
137            fail("IOException expected");
138        } catch (IOException expected) {
139        }
140
141        verify(mock);
142
143        try {
144            mock.throwsIOException(0);
145            fail("AssertionError expected");
146        } catch (AssertionError expected) {
147            assertEquals("\n  Unexpected method call IMethods.throwsIOException(0 (int)):"
148                    + "\n    IMethods.throwsIOException(0 (int)): expected: 2, actual: 3", expected.getMessage());
149        }
150    }
151
152    @Test
153    public void manyMocks() {
154        IMethods otherMock = mock(IMethods.class);
155        expect(otherMock.oneArg(1)).andReturn("test");
156        replay(mock, otherMock);
157
158        try {
159            verify(mock, otherMock);
160            fail("Should fail on otherMock");
161        } catch (AssertionError e) {
162            assertEquals(AssertionError.class, e.getClass());
163            assertEquals("On mock #1 (zero indexed): \n  Expectation failure on verify:\n    IMethods.oneArg(1 (int)): expected: 1, actual: 0", e.getMessage());
164        }
165    }
166
167    @Test
168    public void callExtraMethodsCheckedInVerify() {
169        IMethods mock = mock(IMethods.class);
170        replay(mock);
171
172        try {
173            mock.simpleMethod();
174        } catch(AssertionError e) {
175            // eat the exception and so prevent the test from failing
176        }
177
178        try {
179            mock.oneArg(1);
180        } catch(AssertionError e) {
181            // eat the exception and so prevent the test from failing
182        }
183
184        // the verify should notice an assertion failed earlier
185        try {
186            verify(mock);
187            fail("Should find unexpected calls");
188        } catch(AssertionError e) {
189            assertEquals("\n  Unexpected method calls:" +
190                "\n    IMethods.simpleMethod()" +
191                "\n    IMethods.oneArg(1 (int))", e.getMessage());
192        }
193    }
194
195    @Test
196    public void uncalledAndUnexpected() {
197        IMethods mock = mock(IMethods.class);
198        expect(mock.oneArg(1)).andReturn("test");
199        replay(mock);
200
201        try {
202            mock.simpleMethod();
203        } catch(AssertionError e) {
204            // eat the exception and so prevent the test from failing
205        }
206
207        // the verify should notice an assertion failed earlier
208        try {
209            verify(mock);
210            fail("Should find unexpected calls");
211        } catch(AssertionError e) {
212            assertEquals(
213                "\n  Expectation failure on verify:" +
214                    "\n    IMethods.oneArg(1 (int)): expected: 1, actual: 0" +
215                    "\n  Unexpected method calls:" +
216                    "\n    IMethods.simpleMethod()", e.getMessage());
217        }
218    }
219
220    @Test
221    public void verifyRecordingTest() {
222        IMethods mock = mock(IMethods.class);
223        expect(mock.oneArg(1)).andReturn("test");
224        replay(mock);
225
226        try {
227            verifyRecording(mock);
228            fail("Should see unused expectations");
229        } catch(AssertionError e) {
230            assertEquals(
231                "\n  Expectation failure on verify:" +
232                    "\n    IMethods.oneArg(1 (int)): expected: 1, actual: 0", e.getMessage());
233        }
234    }
235
236    @Test
237    public void verifyUnexpectedCallsTest() {
238        IMethods mock = mock(IMethods.class);
239        replay(mock);
240
241        try {
242            mock.simpleMethod();
243        } catch(AssertionError e) {
244            // eat the exception and so prevent the test from failing
245        }
246
247        // the verify should notice an assertion failed earlier
248        try {
249            verifyUnexpectedCalls(mock);
250            fail("Should find unexpected calls");
251        } catch(AssertionError e) {
252            assertEquals("\n  Unexpected method calls:" +
253                    "\n    IMethods.simpleMethod()", e.getMessage());
254        }
255    }
256}
257
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)