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

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

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.tests;
17
18import org.easymock.MockType;
19import org.easymock.internal.MocksControl;
20import org.junit.Test;
21
22import java.util.List;
23
24import static org.easymock.EasyMock.*;
25import static org.junit.Assert.*;
26
27/**
28 * Test all kind of mocking making sure the partial mocking and interface works
29 * and that to correct behavior is given.
30 *
31 * @author Henri Tremblay
32 */
33public class MockingTest {
34
35    public static class ClassToMock {
36        public int foo() {
37            return 10;
38        }
39
40        public int method() {
41            return 20;
42        }
43    }
44
45    /**
46     * Make sure one mock is not interacting with another
47     */
48    @Test
49    public void testTwoMocks() {
50        ClassToMock transition1 = createMock(ClassToMock.class);
51        ClassToMock transition2 = createMock(ClassToMock.class);
52
53        // Should have two different callbacks
54        assertNotSame(MocksControl.getInvocationHandler(transition2),
55                MocksControl.getInvocationHandler(transition1));
56
57        transition2.foo();
58        transition1.foo();
59    }
60
61    @Test
62    public void testInterfaceMocking() {
63        checkInterfaceMock(createMock(List.class), MockType.DEFAULT);
64    }
65
66    @Test
67    public void testNiceInterfaceMocking() {
68        checkInterfaceMock(createNiceMock(List.class), MockType.NICE);
69    }
70
71    @Test
72    public void testStrictInterfaceMocking() {
73        checkInterfaceMock(createStrictMock(List.class), MockType.STRICT);
74    }
75
76    @Test
77    public void testClassMocking() {
78        checkClassMocking(createMock(ClassToMock.class), MockType.DEFAULT);
79    }
80
81    @Test
82    public void testStrictClassMocking() {
83        checkClassMocking(createStrictMock(ClassToMock.class), MockType.STRICT);
84    }
85
86    @Test
87    public void testNiceClassMocking() {
88        checkClassMocking(createNiceMock(ClassToMock.class), MockType.NICE);
89    }
90
91    @Test
92    public void testMockingNull() {
93        try {
94            createMock(null);
95            fail("Should throw a NPE");
96        } catch(NullPointerException e) {
97            assertEquals("Can't mock 'null'", e.getMessage());
98        }
99    }
100
101    private void checkInterfaceMock(Object mock, MockType behavior) {
102        checkBehavior(mock, behavior);
103    }
104
105    private void checkClassMocking(Object mock, MockType behavior) {
106        checkBehavior(mock, behavior);
107    }
108
109    private void checkBehavior(Object mock, MockType behavior) {
110        assertEquals(behavior, extractBehavior(mock));
111    }
112
113    private MockType extractBehavior(Object mock) {
114        MocksControl ctrl = MocksControl.getControl(mock);
115        return ctrl.getType();
116    }
117}
118
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)