How to use method of org.easymock.tests.DefaultClassInstantiatorTest class

Best Easymock code snippet using org.easymock.tests.DefaultClassInstantiatorTest.

Run Easymock automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright 2001-2021 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 java.io.Serializable;
19
20import org.easymock.internal.ClassInstantiatorFactory;
21import org.easymock.internal.DefaultClassInstantiator;
22import org.junit.AfterClass;
23import org.junit.BeforeClass;
24import org.junit.Ignore;
25import org.junit.Test;
26
27import static org.easymock.EasyMock.*;
28import static org.junit.Assert.*;
29
30/**
31 * Class testing the default instantiator. I'm cheating a little bit here since
32 * I'm not unit testing directly the class. The reason I'm doing this is that I
33 * want to make sure it works well with the cglib class and not the actual
34 * mocked class.
35 * 
36 * @author Henri Tremblay
37 */
38public class DefaultClassInstantiatorTest {
39
40    public static class PrimitiveParamClass {
41        public PrimitiveParamClass(int i) {
42        }
43    }
44
45    public static class FinalParamClass {
46        public FinalParamClass(String i) {
47        }
48    }
49
50    public static class ProtectedConstructorClass {
51        protected ProtectedConstructorClass() {
52        }
53    }
54
55    public static class ProtectedWithPrimitiveConstructorClass {
56        protected ProtectedWithPrimitiveConstructorClass(int i) {
57        }
58    }
59
60    public static class ParamClass {
61        public ParamClass(FinalParamClass f) {
62        }
63    }
64
65    public static class ObjectClass {
66        public ObjectClass(Object c) {
67        }
68    }
69
70    public static class ObjectParamClass {
71        public ObjectParamClass(ParamClass c) {
72        }
73    }
74
75    public static class PrivateConstructorClass {
76        private PrivateConstructorClass() {
77        }
78    }
79
80    public static class ConstructorWithCodeClass {
81        public ConstructorWithCodeClass() {
82            throw new RuntimeException();
83        }
84    }
85
86    @SuppressWarnings("serial")
87    public static class SerializableClass implements Serializable {
88        public SerializableClass() {
89            throw new RuntimeException();
90        }
91    }
92
93    public static class SerializableWithUIDClass implements Serializable {
94
95        private static final long serialVersionUID = -1;
96
97        public SerializableWithUIDClass() {
98            throw new RuntimeException();
99        }
100    }
101
102    @SuppressWarnings("serial")
103    public static class BadlyDoneSerializableClass implements Serializable {
104
105        private final long serialVersionUID = 2; // not static
106
107        public BadlyDoneSerializableClass() {
108            throw new RuntimeException();
109        }
110    }
111
112    private final String vendor = null;
113
114    @BeforeClass
115    public static void setUp() {
116        // Set the default instantiator
117        ClassInstantiatorFactory.setInstantiator(new DefaultClassInstantiator());
118    }
119
120    @AfterClass
121    public static void tearDown() {
122        // Set the value back to be clean
123        ClassInstantiatorFactory.setDefaultInstantiator();
124    }
125
126    @Test
127    public void emptyConstructor() {
128        checkInstantiation(DefaultClassInstantiator.class);
129    }
130
131    @Test
132    public void primitiveType() {
133        checkInstantiation(PrimitiveParamClass.class);
134    }
135
136    @Test
137    @Ignore // Fails on Java 7 for a currently unknown reason
138    public void finalType() {
139        checkInstantiation(FinalParamClass.class);
140    }
141
142    @Test
143    public void protectedConstructor() {
144        checkInstantiation(ProtectedConstructorClass.class);
145    }
146
147    @Test
148    public void protectedWithPrimitiveConstructor() {
149        checkInstantiation(ProtectedWithPrimitiveConstructorClass.class);
150    }
151
152    @Test
153    public void object() {
154        checkInstantiation(ObjectClass.class);
155    }
156
157    @Test
158    @Ignore // Fails on Java 7 for a currently unknown reason
159    public void objectParamRecursion() {
160        checkInstantiation(ObjectParamClass.class);
161    }
162
163    @Test
164    public void constructorWithCodeLimitation() {
165        try {
166            createMock(ConstructorWithCodeClass.class);
167            fail("Shouldn't be possible to mock, code in constructor should crash");
168        } catch (Exception e) {
169        }
170    }
171
172    @Test
173    public void privateConstructorLimitation() {
174        try {
175            createMock(PrivateConstructorClass.class);
176            fail("Shouldn't be able to mock a class with a private constructor using DefaultInstantiator");
177        } catch (Exception e) {
178        }
179    }
180
181    @Test
182    public void privateConstructor() {
183        DefaultClassInstantiator instantiator = new DefaultClassInstantiator();
184        try {
185            instantiator.newInstance(PrivateConstructorClass.class);
186            fail("Shouldn't be able to mock a class with a private constructor using DefaultInstantiator");
187        } catch (Exception e) {
188        }
189    }
190
191    @Test
192    public void newInstance() {
193        checkInstantiation(DefaultClassInstantiator.class);
194    }
195
196    @Test
197    public void serializable() {
198        checkInstantiation(SerializableClass.class);
199    }
200
201    @Test
202    public void badSerializable() throws Exception {
203        DefaultClassInstantiator instantiator = new DefaultClassInstantiator();
204        assertTrue(instantiator.newInstance(BadlyDoneSerializableClass.class) instanceof BadlyDoneSerializableClass);
205    }
206
207    @Test
208    public void serializableWithUID() throws Exception {
209        DefaultClassInstantiator instantiator = new DefaultClassInstantiator();
210        assertTrue(instantiator.newInstance(SerializableWithUIDClass.class) instanceof SerializableWithUIDClass);
211    }
212
213    private <T> void checkInstantiation(Class<T> clazz) {
214        T mock = createMock(clazz);
215        assertTrue(clazz.isAssignableFrom(mock.getClass()));
216    }
217}
218
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)