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

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

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 UsageCallCountTest {
28
29    private VoidMethodInterface mock;
30
31    private interface VoidMethodInterface {
32        void method();
33    }
34
35    @Before
36    public void setup() {
37        mock = createMock(VoidMethodInterface.class);
38    }
39
40    @Test
41    public void mockWithNoExpectedCallsPassesWithNoCalls() {
42        replay(mock);
43        verify(mock);
44    }
45
46    @Test
47    public void mockWithNoExpectedCallsFailsAtFirstCall() {
48        replay(mock);
49        assertMethodCallFails();
50    }
51
52    @Test
53    public void mockWithOneExpectedCallFailsAtVerify() {
54        callMethodOnce();
55        replay(mock);
56        assertVerifyFails();
57    }
58
59    @Test
60    public void mockWithOneExpectedCallPassesWithOneCall() {
61        callMethodOnce();
62        replay(mock);
63        callMethodOnce();
64        verify(mock);
65    }
66
67    @Test
68    public void mockWithOneExpectedCallFailsAtSecondCall() {
69        callMethodOnce();
70        replay(mock);
71        callMethodOnce();
72        assertMethodCallFails();
73    }
74
75    @Test
76    public void tooFewCalls() {
77        callMethodThreeTimes();
78        replay(mock);
79        callMethodTwice();
80        assertVerifyFails();
81    }
82
83    @Test
84    public void correctNumberOfCalls() {
85        callMethodThreeTimes();
86        replay(mock);
87        callMethodThreeTimes();
88        verify(mock);
89    }
90
91    @Test
92    public void tooManyCalls() {
93        callMethodThreeTimes();
94        replay(mock);
95        callMethodThreeTimes();
96        assertMethodCallFails();
97    }
98
99    private void callMethodOnce() {
100        mock.method();
101    }
102
103    private void callMethodTwice() {
104        mock.method();
105        mock.method();
106    }
107
108    private void callMethodThreeTimes() {
109        mock.method();
110        mock.method();
111        mock.method();
112    }
113
114    private void assertVerifyFails() {
115        try {
116            verify(mock);
117            fail("Expected AssertionError");
118        } catch (final AssertionError expected) {
119        }
120    }
121
122    private void assertMethodCallFails() {
123        try {
124            mock.method();
125            fail("Expected AssertionError");
126        } catch (final AssertionError expected) {
127        }
128    }
129
130    @Test
131    public void noUpperLimitWithoutCallCountSet() {
132        mock.method();
133        expectLastCall().atLeastOnce();
134        replay(mock);
135        assertVerifyFails();
136        mock.method();
137        verify(mock);
138        mock.method();
139        verify(mock);
140        mock.method();
141        verify(mock);
142    }
143}
144
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)