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

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

Run Easymock automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright 2012-2013 Alistair Todd
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 com.googlecode.easymockrule;
17
18import java.lang.reflect.Field;
19import java.lang.reflect.Proxy;
20
21import org.apache.commons.lang.StringUtils;
22import org.easymock.internal.MocksControl;
23import org.easymock.internal.MocksControl.MockType;
24import org.easymock.internal.ObjectMethodsFilter;
25
26/**
27 * Utility methods for working with EasyMock.
28 * 
29 * @author Alistair Todd <ringracer@gmail.com>
30 */
31public class EasyMockUtils {
32
33	private EasyMockUtils() {
34		// Hide utility class constructor
35	}
36
37	/**
38	 * Get the control owning the supplied mock. Will blow up if the object
39	 * passed is not a mock.
40	 * 
41	 * @param mock
42	 * @return

43	 */
44	public static MocksControl getMockControl(Object mock) {
45		return getInvocationHandler(mock).getDelegate().getControl();
46	}
47
48	private static ObjectMethodsFilter getInvocationHandler(Object mock) {
49		return (ObjectMethodsFilter) Proxy.getInvocationHandler(mock);
50	}
51
52	/**
53	 * Get the mock control "type" field value or empty string on error.
54	 * 
55	 * @param mock
56	 * @return

57	 */
58	public static String getMockType(Object mock) {
59
60		try {
61			MocksControl mockControl = getMockControl(mock);
62
63			Field mockTypeField = mockControl.getClass().getDeclaredField("type");
64			mockTypeField.setAccessible(true);
65
66			MockType mockType = (MockType) mockTypeField.get(mockControl);
67
68			return mockType.toString();
69
70		} catch (Exception e) {
71			return StringUtils.EMPTY;
72		}
73	}
74}
75
Full Screen
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 java.lang.reflect.Method;
22import java.util.List;
23
24import org.easymock.MockType;
25import org.easymock.internal.MocksControl;
26import org.junit.Test;
27
28/**
29 * Test all kind of mocking making sure the partial mocking and interface works
30 * and that to correct behavior is given.
31 * 
32 * @author Henri Tremblay
33 */
34public class MockingTest {
35
36    public static class ClassToMock {
37        public int foo() {
38            return 10;
39        }
40
41        public int method() {
42            return 20;
43        }
44    }
45
46    /**
47     * Make sure one mock is not interacting with another
48     */
49    @Test
50    public void testTwoMocks() {
51        final ClassToMock transition1 = createMock(ClassToMock.class);
52        final ClassToMock transition2 = createMock(ClassToMock.class);
53
54        // Should have two different callbacks
55        assertNotSame(MocksControl.getInvocationHandler(transition2),
56                MocksControl.getInvocationHandler(transition1));
57
58        transition2.foo();
59        transition1.foo();
60    }
61
62    @Test
63    public void testInterfaceMocking() {
64        checkInterfaceMock(createMock(List.class), MockType.DEFAULT);
65    }
66
67    @Test
68    public void testNiceInterfaceMocking() {
69        checkInterfaceMock(createNiceMock(List.class), MockType.NICE);
70    }
71
72    @Test
73    public void testStrictInterfaceMocking() {
74        checkInterfaceMock(createStrictMock(List.class), MockType.STRICT);
75    }
76
77    @Test
78    public void testClassMocking() {
79        checkClassMocking(createMock(ClassToMock.class), MockType.DEFAULT);
80    }
81
82    @Test
83    public void testStrictClassMocking() {
84        checkClassMocking(createStrictMock(ClassToMock.class), MockType.STRICT);
85    }
86
87    @Test
88    public void testNiceClassMocking() {
89        checkClassMocking(createNiceMock(ClassToMock.class), MockType.NICE);
90    }
91
92    private void checkInterfaceMock(final Object mock, final MockType behavior) {
93        checkBehavior(mock, behavior);
94    }
95
96    @SuppressWarnings("deprecation")
97    @Test
98    public void testPartialClassMocking() {
99        final ClassToMock mock = createMock(ClassToMock.class, getMethod());
100        checkPartialClassMocking(mock, MockType.DEFAULT);
101    }
102
103    @SuppressWarnings("deprecation")
104    @Test
105    public void testStrictPartialClassMocking() {
106        final ClassToMock mock = createStrictMock(ClassToMock.class, getMethod());
107        checkPartialClassMocking(mock, MockType.STRICT);
108    }
109
110    @SuppressWarnings("deprecation")
111    @Test
112    public void testNicePartialClassMocking() {
113        final ClassToMock mock = createNiceMock(ClassToMock.class, getMethod());
114        checkPartialClassMocking(mock, MockType.NICE);
115    }
116
117    private void checkPartialClassMocking(final ClassToMock mock, final MockType behavior) {
118        checkClassMocking(mock, behavior);
119        assertEquals(10, mock.foo());
120        expect(mock.method()).andReturn(30);
121        replay(mock);
122        assertEquals(10, mock.foo());
123        assertEquals(30, mock.method());
124        verify(mock);
125    }
126
127    private void checkClassMocking(final Object mock, final MockType behavior) {
128        checkBehavior(mock, behavior);
129    }
130
131    private void checkBehavior(final Object mock, final MockType behavior) {
132        assertEquals(behavior, extractBehavior(mock));
133    }
134
135    private MockType extractBehavior(final Object mock) {
136        final MocksControl ctrl = MocksControl.getControl(mock);
137        return ctrl.getType();
138    }
139
140    private Method[] getMethod() {
141        try {
142            return new Method[] { ClassToMock.class.getDeclaredMethod("method", (Class[]) null) };
143        } catch (final NoSuchMethodException e) {
144            throw new RuntimeException(e.getMessage());
145        }
146    }
147}
148
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)