use of org.powermock.core.classloader.annotations.UseClassPathAdjuster in project powermock by powermock.
the class MockClassLoaderFactory method createMockClassLoader.
protected ClassLoader createMockClassLoader(final String[] classesToLoadByMockClassloader) {
List<MockTransformer> mockTransformerChain = getMockTransformers(extraMockTransformers);
final UseClassPathAdjuster useClassPathAdjuster = testClass.getAnnotation(UseClassPathAdjuster.class);
ClassLoader mockLoader = AccessController.doPrivileged(new PrivilegedAction<MockClassLoader>() {
@Override
public MockClassLoader run() {
return new MockClassLoader(classesToLoadByMockClassloader, packagesToIgnore, useClassPathAdjuster);
}
});
MockClassLoader mockClassLoader = (MockClassLoader) mockLoader;
mockClassLoader.setMockTransformerChain(mockTransformerChain);
new MockPolicyInitializerImpl(testClass).initialize(mockLoader);
return mockLoader;
}
use of org.powermock.core.classloader.annotations.UseClassPathAdjuster in project powermock by powermock.
the class MockClassLoaderTest method canFindDynamicClassFromAdjustedClasspath.
@Test
public void canFindDynamicClassFromAdjustedClasspath() throws Exception {
// Construct MockClassLoader with @UseClassPathAdjuster annotation.
// It activates our MyClassPathAdjuster class which appends our dynamic
// class to the MockClassLoader's classpool.
UseClassPathAdjuster useClassPathAdjuster = new UseClassPathAdjuster() {
public Class<? extends Annotation> annotationType() {
return UseClassPathAdjuster.class;
}
public Class<? extends ClassPathAdjuster> value() {
return MyClassPathAdjuster.class;
}
};
final MockClassLoader mockClassLoader = new MockClassLoader(new String[0], useClassPathAdjuster);
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 {
if (name.equals(DynamicClassHolder.clazz.getName())) {
return DynamicClassHolder.clazz;
}
return super.loadClass(name);
}
};
// verify that MockClassLoader can successfully load the class
Class<?> dynamicTestClass = Class.forName(DynamicClassHolder.clazz.getName(), false, mockClassLoader);
assertThat(dynamicTestClass).isNotSameAs(DynamicClassHolder.clazz);
}
Aggregations