I see serviceClass classloader type is MockClassLoader. Is there any way to prevent that? Is @PowerMockIgnore the only option?
class A runs with powermock and calls a method of class B via reflection
@RunWith(PowerMockRunner.class)
public class A {
@Test
public void testA() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException{
Class<?> serviceClass = Class.forName(B.class.getName());
Object serviceStub = serviceClass.newInstance();
Method method = serviceClass.getMethod("fun");
method.invoke(serviceStub);
}
}
class B{
public void fun(){
System.out.println("fun!");
}
}
If you load class, then you can specify class loader, for example:
@RunWith(PowerMockRunner.class)
public class A {
@Test
public void testA() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException{
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
Class<?> serviceClass = Class.forName(B.class.getName(), true, classLoader);
Object serviceStub = serviceClass.newInstance();
Method method = serviceClass.getMethod("fun");
method.invoke(serviceStub);
}
}
But if you want that class will not be loaded by MockClassloader
when it is loaded implicitly, then use @PowerMockIgnore
it's the only way.