How to use method of org.jmock.test.acceptance.MockingGenericsFromOtherClassLoadersAcceptanceTests class

Best Jmock-library code snippet using org.jmock.test.acceptance.MockingGenericsFromOtherClassLoadersAcceptanceTests.

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package org.jmock.test.acceptance;
2
3import org.jmock.Expectations;
4import org.jmock.auto.Mock;
5import org.jmock.imposters.ByteBuddyClassImposteriser;
6import org.jmock.integration.junit4.JUnitRuleMockery;
7import org.jmock.testjar.InterfaceFromOtherClassLoader;
8import org.junit.Rule;
9import org.junit.Test;
10import org.junit.jupiter.api.Disabled;
11
12/**
13 * This tests the solution to a longstanding jMock issue. Mock return values for
14 * generic interfaces loaded by other class loaders had the type constraint of
15 * the source interface not the runtime type. The ByteBuddyClassImposteriser
16 * oxposed this issue as it cast mocked method return values to their target
17 * type. This caused ClassCastExceptions. Returning null in the cases where
18 * we're building expectations is the simple solution. Once will(returnValue())
19 * has bee called then the actual mock reply is known and can be returned at
20 * expectation assertion time.
21 * 
22 * @author oliverbye
23 *
24 */
25public class MockingGenericsFromOtherClassLoadersAcceptanceTests {
26
27    @Rule
28    public final JUnitRuleMockery context = new JUnitRuleMockery() {
29        {
30            setImposteriser(ByteBuddyClassImposteriser.INSTANCE);
31        }
32    };
33
34    /**
35     * This test case requires a generic mock.
36     */
37    @Mock
38    public InterfaceFromOtherClassLoader<ABean> mock;
39
40    @SuppressWarnings("unused")
41    @Test
42    public void testWhenDefinedAndInvokedThroughClass() throws Exception {
43
44        context.checking(new Expectations() {
45            {
46                // Assigning the mock is not normal
47                // We do this to prove that the result is assignable to the generic interfaces
48                // runtime type (ABean)
49                ABean bean = oneOf(mock).stir(ABEAN);
50                will(returnValue(ABEAN));
51            }
52        });
53
54        ABean result1 = mock.stir(ABEAN);
55    }
56
57    @SuppressWarnings("unused")
58    @Test
59    public void testWhenDefinedThroughClassAndInvokedThroughMethod() throws Exception {
60
61        context.checking(new Expectations() {
62            {
63                ABean bean = oneOf(mock).stir(ABEAN);
64                will(returnValue(ABEAN));
65            }
66        });
67
68        // Note: this is invoked through a "bridge" method and so the method
69        // invoked when expectations are checked appears to be different from
70        // that invoked when expectations are captured.
71        ABean result1 = ((InterfaceFromOtherClassLoader<ABean>) mock).stir(ABEAN);
72    }
73
74    @Disabled
75    public void DONTtestAndBoxedNativeParameterIgnoingIsADocumentationWhenDefinedThroughClassAndInvokedThroughMethod()
76            throws Exception {
77
78        context.checking(new Expectations() {
79            {
80                oneOf(mock).stir(with(any(Integer.class)));
81                will(returnValue(ABEAN));
82            }
83        });
84
85        // Note: this is invoked through a "bridge" method and so the method
86        // invoked when expectations are checked appears to be different from
87        // that invoked when expectations are captured.
88        ((InterfaceFromOtherClassLoader<ABean>) mock).stir(ABEAN);
89    }
90
91    @SuppressWarnings("unused")
92    @Test
93    public void testWhenDefinedAndInvokedThroughInterface() throws Exception {
94
95        context.checking(new Expectations() {
96            {
97                ABean bean = oneOf(mock).stir(ABEAN);
98                will(returnValue(ABEAN));
99            }
100        });
101
102        ABean result1 = mock.stir(ABEAN);
103    }
104
105    public static abstract class ABean {
106    }
107
108    public ABean ABEAN = new ABean() {
109    };
110}
111
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)