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

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

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.tests2;
17
18import org.easymock.ConstructorArgs;
19import org.easymock.IMocksControl;
20import org.junit.Test;
21
22import java.util.ArrayList;
23import java.util.List;
24
25import static org.easymock.EasyMock.*;
26import static org.junit.Assert.assertEquals;
27
28/**
29 * @author Henri Tremblay
30 */
31public class MocksControlTest {
32
33    public static class A {
34        int i = 1;
35
36        public A(int i) {
37            this.i = i;
38        }
39
40        public int foo() {
41            return bar();
42        }
43
44        public int bar() {
45            return i;
46        }
47
48        public boolean add(int i) {
49            this.i += i;
50            return true;
51        }
52    }
53
54    @Test
55    public void testMocksControl_Interface() {
56        IMocksControl ctrl = createControl();
57        List<?> list = ctrl.createMock(List.class);
58        testList(ctrl, list);
59    }
60
61    @Test
62    public void testMocksControl_Class() {
63        IMocksControl ctrl = createControl();
64        ArrayList<?> list = ctrl.createMock(ArrayList.class);
65        testList(ctrl, list);
66    }
67
68    @Test
69    public void testMocksControl_Class_WithName() {
70        IMocksControl ctrl = createControl();
71        ArrayList<?> list = ctrl.createMock("myMock", ArrayList.class);
72        testList(ctrl, list);
73    }
74
75    @Test
76    public void testMocksControl_PartialMock_NoConstructorCalled() throws Exception {
77        IMocksControl ctrl = createControl();
78        A a = ctrl.createMock(null, A.class, null, A.class.getMethod("bar"), A.class.getMethod(
79                "toString"));
80
81        assertEquals("No constructor called so should not be initialized", 0, a.i);
82        expect(a.bar()).andReturn(5);
83        replay(a);
84        assertEquals("foo isn't mocked so it will call bar which return 5", 5, a.foo());
85        verify(a);
86
87        assertEquals("EasyMock for class org.easymock.tests2.MocksControlTest$A", a.toString());
88    }
89
90    @Test
91    public void testMocksControl_NamedPartialMock_NoConstructorCalled() throws Exception {
92        IMocksControl ctrl = createControl();
93        A a = ctrl.createMock("myMock", A.class, null, A.class.getMethod("bar"), A.class
94                .getMethod("toString"));
95
96        assertEquals("No constructor called so should not be initialized", 0, a.i);
97        expect(a.bar()).andReturn(5);
98        replay(a);
99        assertEquals("foo isn't mocked so it will call bar which return 5", 5, a.foo());
100        verify(a);
101
102        assertEquals("myMock", a.toString());
103    }
104
105    @Test
106    public void testMocksControl_PartialMock_ConstructorCalled() throws Exception {
107        IMocksControl ctrl = createControl();
108
109        ConstructorArgs args = new ConstructorArgs(A.class.getConstructor(Integer.TYPE), 6);
110
111        A a = ctrl.createMock(null, A.class, args, A.class.getMethod("bar"), A.class.getMethod(
112                "toString"));
113
114        assertEquals("Constructor called so should be initialized", 6, a.i);
115        expect(a.bar()).andReturn(5);
116        replay(a);
117        assertEquals("foo isn't mocked so it will call bar which return 5", 5, a.foo());
118        verify(a);
119
120        assertEquals("EasyMock for class org.easymock.tests2.MocksControlTest$A", a.toString());
121    }
122
123    @Test
124    public void testMocksControl_NamedPartialMock_ConstructorCalled() throws Exception {
125        IMocksControl ctrl = createControl();
126
127        ConstructorArgs args = new ConstructorArgs(A.class.getConstructor(Integer.TYPE), 6);
128
129        A a = ctrl.createMock("myMock", A.class, args, A.class.getMethod("bar"), A.class
130                .getMethod("toString"));
131
132        assertEquals("Constructor called so should be initialized", 6, a.i);
133        expect(a.bar()).andReturn(5);
134        replay(a);
135        assertEquals("foo isn't mocked so it will call bar which return 5", 5, a.foo());
136        verify(a);
137
138        assertEquals("myMock", a.toString());
139    }
140
141    private void testList(IMocksControl ctrl, List<?> list) {
142        expect(list.size()).andReturn(3);
143        ctrl.replay();
144        assertEquals(3, list.size());
145        ctrl.verify();
146    }
147
148}
149
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)