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 java.io.File;
4import java.net.URL;
5import java.net.URLClassLoader;
6import java.util.Date;
7
8import junit.framework.TestCase;
9
10import org.jmock.api.Action;
11import org.jmock.api.Imposteriser;
12import org.jmock.lib.action.ReturnValueAction;
13import org.jmock.lib.action.VoidAction;
14import org.jmock.lib.legacy.ClassImposteriser;
15
16public class ClassImposteriserTests extends TestCase {
17    Action action = new ReturnValueAction("result");
18    
19    Imposteriser imposteriser = ClassImposteriser.INSTANCE;
20    
21    public static class ConcreteClassWithNastyConstructor {
22        {
23            nasty("initialisation block should not be run");
24        }
25        
26        public ConcreteClassWithNastyConstructor() {
27            nasty("constructor should not be run");
28        }
29        
30        public String foo() {
31            nasty("should not be run");
32            return null; // never reached
33        }
34
35        private static void nasty(String nastiness) {
36            throw new IllegalStateException(nastiness);
37        }
38    }
39    
40    public interface AnInterface {
41        String foo();
42    }
43    
44    public static abstract class AnAbstractNestedClass {
45        public abstract String foo();
46    }
47    
48    public static class AnInnerClass {
49        public String foo() {return "original result";}
50    }
51    
52    public static final class AFinalClass {
53        public String foo() {return "original result";}
54    }
55    
56    public static class AClassWithAPrivateConstructor {
57        @SuppressWarnings("unused")
58        private AClassWithAPrivateConstructor(String someArgument) {}
59        
60        public String foo() {return "original result";}
61    }
62    
63    public void testCanImposteriseInterfacesAndNonFinalInstantiableClasses() {
64        assertTrue("should report that it can imposterise interfaces",
65                   imposteriser.canImposterise(Runnable.class));
66        assertTrue("should report that it can imposterise classes",
67                   imposteriser.canImposterise(Date.class));
68        assertTrue("should report that it cannot imposterise final classes",
69                   !imposteriser.canImposterise(AFinalClass.class));
70        assertTrue("should report that it can imposterise nested classes",
71                   imposteriser.canImposterise(AnAbstractNestedClass.class));
72        assertTrue("should report that it can imposterise inner classes",
73                   imposteriser.canImposterise(AnInnerClass.class));
74        assertTrue("should report that it can imposterise classes with non-public constructors",
75                   imposteriser.canImposterise(AClassWithAPrivateConstructor.class));
76        assertTrue("should report that it cannot imposterise primitive types",
77                   !imposteriser.canImposterise(int.class));
78        assertTrue("should report that it cannot imposterise void",
79                   !imposteriser.canImposterise(void.class));
80    }
81
82    public void testCanImposteriseAConcreteClassWithoutCallingItsConstructorOrInstanceInitialiserBlocks() {
83        ConcreteClassWithNastyConstructor imposter = 
84            imposteriser.imposterise(action, ConcreteClassWithNastyConstructor.class);
85        
86        assertEquals("result", imposter.foo());
87    }
88    
89    public void testCanImposteriseAnInterface() {
90        AnInterface imposter = 
91            imposteriser.imposterise(action, AnInterface.class);
92        
93        assertEquals("result", imposter.foo());
94    }
95    
96    public void testCanImposteriseAClassWithAPrivateConstructor() {
97        AClassWithAPrivateConstructor imposter = 
98            imposteriser.imposterise(action, AClassWithAPrivateConstructor.class);
99        
100        assertEquals("result", imposter.foo());
101    }
102    
103    public void testCanImposteriseAClassInASignedJarFile() throws Exception {
104        File jarFile = new File("build/testdata/signed.jar");
105        
106        assertTrue("Signed JAR file does not exist (use Ant to build it", jarFile.exists());
107        
108        URL jarURL = jarFile.toURI().toURL();
109        ClassLoader loader = new URLClassLoader(new URL[]{jarURL});
110        Class<?> typeInSignedJar = loader.loadClass("TypeInSignedJar");
111        
112        Object o = imposteriser.imposterise(new VoidAction(), typeInSignedJar);
113        
114        assertTrue(typeInSignedJar.isInstance(o));
115    }
116    
117    public static class ClassWithFinalToStringMethod {
118        @Override
119        public final String toString() {
120            return "you can't override me!";
121        }
122    }
123    
124    // See issue JMOCK-150
125    public void testCannotImposteriseAClassWithAFinalToStringMethod() {
126        assertTrue("should not be able to imposterise it", !imposteriser.canImposterise(ClassWithFinalToStringMethod.class));
127        
128        try {
129            imposteriser.imposterise(new VoidAction(), ClassWithFinalToStringMethod.class);
130            fail("should have thrown IllegalArgumentException");
131        }
132        catch (IllegalArgumentException expected) {
133            
134        }
135    }
136    
137    public interface EmptyInterface {}
138    
139    // See issue JMOCK-145
140    public void testWorksAroundBugInCglibWhenAskedToImposteriseObject() {
141        imposteriser.imposterise(new VoidAction(), Object.class);
142        
143        imposteriser.imposterise(new VoidAction(), Object.class, EmptyInterface.class);
144        
145        imposteriser.imposterise(new VoidAction(), Object.class, AnInterface.class);
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)