How to use method of org.jmock.test.unit.lib.legacy.ClassImposteriserTests class

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

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.unit.lib.legacy;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertTrue;
5import static org.junit.Assert.fail;
6
7import java.io.File;
8import java.lang.reflect.InvocationTargetException;
9import java.lang.reflect.Method;
10import java.net.URL;
11import java.net.URLClassLoader;
12import java.util.Date;
13
14import org.jmock.api.Action;
15import org.jmock.api.Imposteriser;
16import org.jmock.api.Invocation;
17import org.jmock.api.Invokable;
18import org.jmock.lib.action.ReturnValueAction;
19import org.jmock.lib.action.VoidAction;
20import org.junit.jupiter.params.ParameterizedTest;
21import org.junit.jupiter.params.provider.ArgumentsSource;
22
23public class ClassImposteriserTests {
24    Action action = new ReturnValueAction("result");
25    
26    @SuppressWarnings("ClassInitializerMayBeStatic")
27    public static class ConcreteClassWithNastyConstructor {
28        {
29            nasty("initialisation block should not be run");
30        }
31        
32        public ConcreteClassWithNastyConstructor() {
33            nasty("constructor should not be run");
34        }
35        
36        public String foo() {
37            nasty("should not be run");
38            return null; // never reached
39        }
40
41        private static void nasty(String nastiness) {
42            throw new IllegalStateException(nastiness);
43        }
44    }
45    
46    public interface AnInterface {
47        String foo();
48    }
49    
50    public static abstract class AnAbstractNestedClass {
51        @SuppressWarnings("UnusedDeclaration")
52        public abstract String foo();
53    }
54    
55    public static class AnInnerClass {
56        @SuppressWarnings("UnusedDeclaration")
57        public String foo() {return "original result";}
58    }
59    
60    public static final class AFinalClass {
61        @SuppressWarnings("UnusedDeclaration")
62        public String foo() {return "original result";}
63    }
64    
65    public static class AClassWithAPrivateConstructor {
66        @SuppressWarnings("unused")
67        private AClassWithAPrivateConstructor(String someArgument) {}
68        
69        public String foo() {return "original result";}
70    }
71    
72    @ParameterizedTest
73    @ArgumentsSource(CodeGeneratingImposteriserParameterResolver.class)
74    public void canImposteriseInterfacesAndNonFinalInstantiableClasses(Imposteriser imposteriser) {
75        assertTrue("should report that it can imposterise interfaces",
76                   imposteriser.canImposterise(Runnable.class));
77        assertTrue("should report that it can imposterise classes",
78                   imposteriser.canImposterise(Date.class));
79        assertTrue("should report that it cannot imposterise final classes",
80                   !imposteriser.canImposterise(AFinalClass.class));
81        assertTrue("should report that it can imposterise nested classes",
82                   imposteriser.canImposterise(AnAbstractNestedClass.class));
83        assertTrue("should report that it can imposterise inner classes",
84                   imposteriser.canImposterise(AnInnerClass.class));
85        assertTrue("should report that it can imposterise classes with non-public constructors",
86                   imposteriser.canImposterise(AClassWithAPrivateConstructor.class));
87        assertTrue("should report that it cannot imposterise primitive types",
88                   !imposteriser.canImposterise(int.class));
89        assertTrue("should report that it cannot imposterise void",
90                   !imposteriser.canImposterise(void.class));
91    }
92
93    @ParameterizedTest
94    @ArgumentsSource(CodeGeneratingImposteriserParameterResolver.class)
95    public void canImposteriseAConcreteClassWithoutCallingItsConstructorOrInstanceInitialiserBlocks(Imposteriser imposteriser) {
96        ConcreteClassWithNastyConstructor imposter = 
97            imposteriser.imposterise(action, ConcreteClassWithNastyConstructor.class);
98        
99        assertEquals("result", imposter.foo());
100    }
101
102    @ParameterizedTest
103    @ArgumentsSource(CodeGeneratingImposteriserParameterResolver.class)
104    public void canImposteriseAnInterface(Imposteriser imposteriser) {
105        AnInterface imposter = 
106            imposteriser.imposterise(action, AnInterface.class);
107        
108        assertEquals("result", imposter.foo());
109    }
110
111    @ParameterizedTest
112    @ArgumentsSource(CodeGeneratingImposteriserParameterResolver.class)
113    public void canImposteriseAClassWithAPrivateConstructor(Imposteriser imposteriser) {
114        AClassWithAPrivateConstructor imposter = 
115            imposteriser.imposterise(action, AClassWithAPrivateConstructor.class);
116        
117        assertEquals("result", imposter.foo());
118    }
119
120    @ParameterizedTest
121    @ArgumentsSource(CodeGeneratingImposteriserParameterResolver.class)
122    public void canImposteriseAClassInASignedJarFile(Imposteriser imposteriser) throws Exception {
123        File jarFile = new File("../testjar/target/signed.jar");
124        
125        assertTrue("Signed JAR file does not exist (use Ant to build it", jarFile.exists());
126        
127        URL jarURL = jarFile.toURI().toURL();
128        
129        // Can't close as we might be in java 6
130        @SuppressWarnings("resource")
131        URLClassLoader loader = new URLClassLoader(new URL[]{jarURL});
132        
133        Class<?> typeInSignedJar = loader.loadClass("org.jmock.testjar.TypeInSignedJar");
134        
135        Object o = imposteriser.imposterise(new VoidAction(), typeInSignedJar);
136        
137        assertTrue(typeInSignedJar.isInstance(o));
138    }
139    
140    public static class ClassWithFinalToStringMethod {
141        @Override
142        public final String toString() {
143            return "you can't override me!";
144        }
145    }
146    
147    // See issue JMOCK-150
148    @ParameterizedTest
149    @ArgumentsSource(CodeGeneratingImposteriserParameterResolver.class)
150    public void cannotImposteriseAClassWithAFinalToStringMethod(Imposteriser imposteriser) {
151        assertTrue("should not be able to imposterise it", !imposteriser.canImposterise(ClassWithFinalToStringMethod.class));
152        
153        try {
154            imposteriser.imposterise(new VoidAction(), ClassWithFinalToStringMethod.class);
155            fail("should have thrown IllegalArgumentException");
156        }
157        catch (IllegalArgumentException expected) {
158            
159        }
160    }
161
162    // See issue JMOCK-256 (Github #36)
163    @ParameterizedTest
164    @ArgumentsSource(CodeGeneratingImposteriserParameterResolver.class)
165    public void doesntDelegateFinalizeMethod(Imposteriser imposteriser) throws Exception {
166        Invokable failIfInvokedAction = new Invokable() {
167            public Object invoke(Invocation invocation) throws Throwable {
168                fail("invocation should not have happened");
169                return null;
170            }
171        };
172
173        Object imposter = imposteriser.imposterise(failIfInvokedAction, Object.class);
174        invokeMethod(imposter, Object.class.getDeclaredMethod("finalize"));
175    }
176
177    public interface EmptyInterface {}
178    
179    // See issue JMOCK-145
180    @ParameterizedTest
181    @ArgumentsSource(CodeGeneratingImposteriserParameterResolver.class)
182    public void worksAroundBugInCglibWhenAskedToImposteriseObject(Imposteriser imposteriser) {
183        imposteriser.imposterise(new VoidAction(), Object.class);
184        
185        imposteriser.imposterise(new VoidAction(), Object.class, EmptyInterface.class);
186        
187        imposteriser.imposterise(new VoidAction(), Object.class, AnInterface.class);
188    }
189
190    private Object invokeMethod(Object object, Method method, Object... args) throws IllegalAccessException, InvocationTargetException {
191        method.setAccessible(true);
192        return method.invoke(object, args);
193    }
194
195}
196
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)