use of org.powermock.core.transformers.impl.ClassMockTransformer in project powermock by powermock.
the class PowerMockClassloaderExecutor method forClass.
public static SingleClassloaderExecutor forClass(Class<?> testClass, MockPolicyInitializer mockPolicyInitializer) {
List<MockTransformer> mockTransformerChain = new ArrayList<MockTransformer>();
mockTransformerChain.add(new ClassMockTransformer());
mockTransformerChain.add(new InterfaceMockTransformer());
MockClassLoader mockLoader = new MockClassLoader(new String[0], new String[0]);
mockLoader.setMockTransformerChain(mockTransformerChain);
mockLoader.addIgnorePackage(new PowerMockIgnorePackagesExtractorImpl().getPackagesToIgnore(testClass));
mockLoader.addClassesToModify(new PrepareForTestExtractorImpl().getTestClasses(testClass));
mockLoader.addClassesToModify(new StaticConstructorSuppressExtractorImpl().getTestClasses(testClass));
mockPolicyInitializer.initialize(mockLoader);
return new SingleClassloaderExecutor(mockLoader);
}
use of org.powermock.core.transformers.impl.ClassMockTransformer in project powermock by powermock.
the class MockClassLoaderTest method cannotFindDynamicClassInDeferredClassLoader.
@Test(expected = ClassNotFoundException.class)
public void cannotFindDynamicClassInDeferredClassLoader() throws Exception {
MockClassLoader mockClassLoader = new MockClassLoader(new String[0]);
List<MockTransformer> list = new LinkedList<MockTransformer>();
list.add(new ClassMockTransformer());
mockClassLoader.setMockTransformerChain(list);
// setup custom classloader providing our dynamic class, for MockClassLoader to defer to
mockClassLoader.deferTo = new ClassLoader(getClass().getClassLoader()) {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
return super.loadClass(name);
}
};
//Try to locate and load a class that is not in MockClassLoader.
Class.forName(DynamicClassHolder.clazz.getName(), false, mockClassLoader);
}
use of org.powermock.core.transformers.impl.ClassMockTransformer in project powermock by powermock.
the class MockClassLoaderTest method autoboxingWorks.
@Test
public void autoboxingWorks() throws Exception {
String name = this.getClass().getPackage().getName() + ".HardToTransform";
final MockClassLoader mockClassLoader = new MockClassLoader(new String[] { name });
List<MockTransformer> list = new LinkedList<MockTransformer>();
list.add(new ClassMockTransformer());
mockClassLoader.setMockTransformerChain(list);
Class<?> c = mockClassLoader.loadClass(name);
Object object = c.newInstance();
Whitebox.invokeMethod(object, "run");
assertThat(5).isEqualTo(Whitebox.invokeMethod(object, "testInt"));
assertThat(5L).isEqualTo(Whitebox.invokeMethod(object, "testLong"));
assertThat(5f).isEqualTo(Whitebox.invokeMethod(object, "testFloat"));
assertThat(5.0).isEqualTo(Whitebox.invokeMethod(object, "testDouble"));
assertThat(new Short("5")).isEqualTo(Whitebox.invokeMethod(object, "testShort"));
assertThat(new Byte("5")).isEqualTo(Whitebox.invokeMethod(object, "testByte"));
assertThat(true).isEqualTo(Whitebox.invokeMethod(object, "testBoolean"));
assertThat('5').isEqualTo(Whitebox.invokeMethod(object, "testChar"));
assertThat("5").isEqualTo(Whitebox.invokeMethod(object, "testString"));
}
use of org.powermock.core.transformers.impl.ClassMockTransformer in project powermock by powermock.
the class ClassLoaderFactory method getMockTransformers.
private List<MockTransformer> getMockTransformers() {
List<MockTransformer> mockTransformerChain = new ArrayList<MockTransformer>();
mockTransformerChain.add(new ClassMockTransformer());
mockTransformerChain.add(new InterfaceMockTransformer());
return mockTransformerChain;
}
use of org.powermock.core.transformers.impl.ClassMockTransformer in project powermock by powermock.
the class MockClassLoaderFactory method getMockTransformers.
protected List<MockTransformer> getMockTransformers(MockTransformer[] extraMockTransformers) {
List<MockTransformer> mockTransformerChain = new ArrayList<MockTransformer>();
mockTransformerChain.add(new ClassMockTransformer());
mockTransformerChain.add(new InterfaceMockTransformer());
Collections.addAll(mockTransformerChain, extraMockTransformers);
return mockTransformerChain;
}
Aggregations