PowerMock works by instrumenting the method calls at runtime, catching the calls and manipulating them as needed. For this, you need to change the Runner class for the unit test. This is done with the @RunWith() annotation on your test class

@RunWith(PowerMockRunner.class)

But what if your test needs the Spring Context up and running before testing?

DatabaseCreateReplJob is an object which inside call(), it calls a method on the sysFacade object. Because this is a call to an external resource (such as a database or something else) we need to mock the response to sysFacade.createReplDBLink().

If we run the code as it is now, we get an error like this:

Caused by: java.lang.LinkageError: loader constraint violation: loader (instance of org/powermock/core/classloader/MockClassLoader) previously initiated loading for a different type with name "javax/management/MBeanServer"
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:262)
at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:206)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass1(DeferSupportingClassLoader.java:89)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:79)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetPublicMethods(Class.java:2902)
at java.lang.Class.privateGetPublicMethods(Class.java:2917)
at java.lang.Class.privateGetPublicMethods(Class.java:2917)
at java.lang.Class.privateGetPublicMethods(Class.java:2917)
at java.lang.Class.getMethods(Class.java:1615)

This happens because PowerMockRunner uses its own classloader to load classes and it interferes with the one in SpringRunner.

This is only an error which might occur. Depending on what your application is loading, different errors can happen.

The solution to all of this is to indicate to PowerMock which classes to ignore when classloading.

This is done with the @PowerMockIgnore annotation.

Example:

@PowerMockIgnore({"javax.*.*", "com.sun.*", "org.xml.*"})

If we add this one annotation to the test class we will have a successful test.