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

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

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.internal.ReplayState;
11import org.easymock.tests.IMethods;
12import org.easymock.tests.Util;
13import org.junit.Before;
14import org.junit.Test;
15
16public class UsageStrictMockTest {
17    private IMethods mock;
18
19    @Before
20    public void setup() {
21        mock = createStrictMock(IMethods.class);
22        mock.simpleMethodWithArgument("1");
23        mock.simpleMethodWithArgument("2");
24        replay(mock);
25    }
26
27    @Test
28    public void orderedCallsSucces() {
29        mock.simpleMethodWithArgument("1");
30        mock.simpleMethodWithArgument("2");
31        verify(mock);
32    }
33
34    @Test
35    public void unorderedCallsFailure() {
36        boolean failed = false;
37        try {
38            mock.simpleMethodWithArgument("2");
39        } catch (AssertionError expected) {
40            failed = true;
41        }
42        if (!failed) {
43            fail("unordered calls accepted");
44        }
45    }
46
47    @Test
48    public void tooManyCallsFailure() {
49        mock.simpleMethodWithArgument("1");
50        mock.simpleMethodWithArgument("2");
51
52        boolean failed = false;
53        try {
54            mock.simpleMethodWithArgument("2");
55        } catch (AssertionError expected) {
56            failed = true;
57        }
58        if (!failed) {
59            fail("too many calls accepted");
60        }
61    }
62
63    @Test
64    public void tooFewCallsFailure() {
65        mock.simpleMethodWithArgument("1");
66        boolean failed = false;
67        try {
68            verify(mock);
69        } catch (AssertionError expected) {
70            failed = true;
71            assertTrue("stack trace must be filled in", Util.getStackTrace(expected).indexOf(
72                    ReplayState.class.getName()) == -1);
73        }
74        if (!failed) {
75            fail("too few calls accepted");
76        }
77    }
78
79    @Test
80    public void differentMethods() {
81
82        reset(mock);
83
84        mock.booleanReturningMethod(0);
85        expectLastCall().andReturn(true);
86        mock.simpleMethod();
87        mock.booleanReturningMethod(1);
88        expectLastCall().andReturn(false).times(2, 3);
89        mock.simpleMethod();
90        expectLastCall().atLeastOnce();
91
92        replay(mock);
93        assertEquals(true, mock.booleanReturningMethod(0));
94        mock.simpleMethod();
95
96        boolean failed = false;
97        try {
98            verify(mock);
99        } catch (AssertionError expected) {
100            failed = true;
101            assertEquals("\n  Expectation failure on verify:" + "\n    simpleMethod(): expected: 1, actual: 1"
102                    + "\n    booleanReturningMethod(1): expected: between 2 and 3, actual: 0"
103                    + "\n    simpleMethod(): expected: at least 1, actual: 0", expected.getMessage());
104        }
105        if (!failed) {
106            fail("too few calls accepted");
107        }
108
109        assertEquals(false, mock.booleanReturningMethod(1));
110
111        failed = false;
112        try {
113            mock.simpleMethod();
114        } catch (AssertionError expected) {
115            failed = true;
116            assertEquals("\n  Unexpected method call simpleMethod():"
117                    + "\n    booleanReturningMethod(1): expected: between 2 and 3, actual: 1", expected.getMessage());
118        }
119        if (!failed) {
120            fail("wrong call accepted");
121        }
122    }
123
124    @Test
125    public void range() {
126
127        reset(mock);
128
129        mock.booleanReturningMethod(0);
130        expectLastCall().andReturn(true);
131        mock.simpleMethod();
132        mock.booleanReturningMethod(1);
133        expectLastCall().andReturn(false).times(2, 3);
134        mock.simpleMethod();
135        expectLastCall().atLeastOnce();
136        expect(mock.booleanReturningMethod(1)).andReturn(false);
137
138        replay(mock);
139
140        mock.booleanReturningMethod(0);
141        mock.simpleMethod();
142
143        mock.booleanReturningMethod(1);
144        mock.booleanReturningMethod(1);
145        mock.booleanReturningMethod(1);
146
147        boolean failed = false;
148
149        try {
150            mock.booleanReturningMethod(1);
151        } catch (AssertionError expected) {
152            failed = true;
153            assertEquals("\n  Unexpected method call booleanReturningMethod(1):"
154                    + "\n    booleanReturningMethod(1): expected: between 2 and 3, actual: 3 (+1)"
155                    + "\n    simpleMethod(): expected: at least 1, actual: 0", expected.getMessage());
156        }
157        if (!failed) {
158            fail("too many calls accepted");
159        }
160    }
161
162    @Test
163    public void stubBehavior() {
164        reset(mock);
165
166        mock.booleanReturningMethod(1);
167        expectLastCall().andReturn(true).andReturn(false).andReturn(true);
168        mock.booleanReturningMethod(anyInt());
169        expectLastCall().andStubReturn(true);
170
171        replay(mock);
172
173        assertEquals(true, mock.booleanReturningMethod(2));
174        assertEquals(true, mock.booleanReturningMethod(3));
175        assertEquals(true, mock.booleanReturningMethod(1));
176        assertEquals(false, mock.booleanReturningMethod(1));
177        assertEquals(true, mock.booleanReturningMethod(3));
178
179        boolean failed = false;
180        try {
181            verify(mock);
182        } catch (AssertionError expected) {
183            failed = true;
184            assertEquals("\n  Expectation failure on verify:"
185                    + "\n    booleanReturningMethod(1): expected: 3, actual: 2", expected.getMessage());
186        }
187        if (!failed) {
188            fail("too few calls accepted");
189        }
190    }
191}
192
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)