How to use CodeGeneratingImposteriserParameterResolver class of org.jmock.test.unit.lib.legacy package

Best Jmock-library code snippet using org.jmock.test.unit.lib.legacy.CodeGeneratingImposteriserParameterResolver

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 static org.junit.Assert.assertEquals;
4
5import java.util.Vector;
6
7import org.jmock.Expectations;
8import org.jmock.Mockery;
9import org.jmock.api.ExpectationError;
10import org.jmock.api.Imposteriser;
11import org.jmock.test.unit.lib.legacy.CodeGeneratingImposteriserParameterResolver;
12import org.jmock.test.unit.lib.legacy.ImposteriserParameterResolver;
13import org.junit.jupiter.params.ParameterizedTest;
14import org.junit.jupiter.params.provider.ArgumentsSource;
15
16// Fixes issue JMOCK-96
17public class RedeclaredObjectMethodsAcceptanceTests {
18
19    Mockery context = new Mockery();
20
21    public interface MockedInterface {
22        String toString();
23    }
24
25    public static class MockedClass {
26        @Override
27        public String toString() {
28            return "not mocked";
29        }
30    }
31
32    @ParameterizedTest
33    @ArgumentsSource(ImposteriserParameterResolver.class)
34    public void testCanRedeclareObjectMethodsInMockedInterfaces(Imposteriser imposteriserImpl) {
35        context.setImposteriser(imposteriserImpl);
36        MockedInterface mock = context.mock(MockedInterface.class, "X");
37
38        assertEquals("X", mock.toString());
39    }
40
41    @ParameterizedTest
42    @ArgumentsSource(CodeGeneratingImposteriserParameterResolver.class)
43    public void testCanRedeclareObjectMethodsInMockedClasses(Imposteriser imposteriserImpl) {
44
45        context.setImposteriser(imposteriserImpl);
46        MockedClass mock = context.mock(MockedClass.class, "X");
47
48        assertEquals("X", mock.toString());
49    }
50
51    /*
52     * Adapted from Jira issue JMOCK-96
53     */
54    @SuppressWarnings("unchecked")
55    @ParameterizedTest
56    @ArgumentsSource(CodeGeneratingImposteriserParameterResolver.class)
57    public void testUseMockObjectHangs1(Imposteriser imposteriserImpl) {
58
59        context.setImposteriser(imposteriserImpl);
60        final Vector<Object> mock = (Vector<Object>) context.mock(Vector.class);
61
62        context.checking(new Expectations() {
63            {
64                atLeast(1).of(mock).size();
65                will(returnValue(2));
66            }
67        });
68
69        try {
70            for (int i = 0; i < mock.size(); i++) {
71                System.out.println("Vector entry " + i + " = " + mock.get(i));
72            }
73        } catch (ExpectationError error) {
74            // expected
75        }
76    }
77}
78
Full Screen
copy
1package org.jmock.test.unit.lib.legacy;
2
3import org.jmock.imposters.ByteBuddyClassImposteriser;
4import org.jmock.lib.legacy.ClassImposteriser;
5import org.jmock.test.acceptance.AbstractImposteriserParameterResolver;
6
7public class CodeGeneratingImposteriserParameterResolver extends AbstractImposteriserParameterResolver {
8
9    public CodeGeneratingImposteriserParameterResolver() {
10        super(ByteBuddyClassImposteriser.INSTANCE,
11                ClassImposteriser.INSTANCE);
12    }
13}
14
Full Screen
copy
1package org.jmock.test.acceptance;
2
3import static org.junit.Assert.assertSame;
4
5import org.jmock.Expectations;
6import org.jmock.Mockery;
7import org.jmock.api.Imposteriser;
8import org.jmock.test.unit.lib.legacy.CodeGeneratingImposteriserParameterResolver;
9import org.jmock.test.unit.lib.legacy.ImposteriserParameterResolver;
10import org.jmock.testjar.InterfaceFromOtherClassLoader;
11import org.junit.Assert;
12import org.junit.jupiter.params.ParameterizedTest;
13import org.junit.jupiter.params.provider.ArgumentsSource;
14
15public class MockingClassesAcceptanceTests {
16
17    public static final class FinalClass {
18    }
19
20    public static class ClassToMock {
21        public FinalClass returnInstanceOfFinalClass() {
22            return null;
23        }
24    }
25
26    Mockery context = new Mockery();
27
28    @ParameterizedTest
29    @ArgumentsSource(CodeGeneratingImposteriserParameterResolver.class)
30    public void testCanMockClassesWithMethodsThatReturnFinalClasses(Imposteriser imposteriserImpl) {
31        context.setImposteriser(imposteriserImpl);
32
33        final ClassToMock mock = context.mock(ClassToMock.class);
34        final FinalClass result = new FinalClass();
35
36        context.checking(new Expectations() {
37            {
38                oneOf(mock).returnInstanceOfFinalClass();
39                will(returnValue(result));
40            }
41        });
42
43        // This should not crash
44
45        assertSame(result, mock.returnInstanceOfFinalClass());
46    }
47    
48    @SuppressWarnings("rawtypes")
49    @ParameterizedTest
50    @ArgumentsSource(ImposteriserParameterResolver.class)
51    public void testMockClassIsCached(Imposteriser imposteriserImpl) {
52        Class class1 = context.mock(InterfaceFromOtherClassLoader.class,"1").getClass();
53        Class class2 = context.mock(InterfaceFromOtherClassLoader.class,"2").getClass();
54        Assert.assertEquals("Type should be cached", class1, class2);
55    }
56}
57
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

...Most popular Stackoverflow questions on CodeGeneratingImposteriserParameterResolver

    No relevant questions found for this class 😞

Most used methods in CodeGeneratingImposteriserParameterResolver

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)