Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powermock driven tests run just fine via maven but don't run in intellij

I have a very large project where and at some point developers started noticing that if they try to run/debug a PowerMock driven test in intellij- they get the following error:

java.lang.NoClassDefFoundError: org/powermock/core/transformers/impl/MainMockTransformer
FATAL ERROR in native method: processing of -javaagent failed
    at org.powermock.modules.agent.PowerMockClassTransformer.<clinit>(PowerMockClassTransformer.java:41)
    at org.powermock.modules.agent.PowerMockAgent.<clinit>(PowerMockAgent.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:386)
    at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
Caused by: java.lang.ClassNotFoundException: org.powermock.core.transformers.impl.MainMockTransformer
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 8 more

We are using the powermockagent, and a unit test usually looks like this:

@PrepareForTest({Bla.class})
public class BlaTest {

    @Rule
    public PowerMockRule rule = new PowerMockRule();

    static {
        PowerMockAgent.initializeIfNeeded();
    }

    @Test public void test() { ... }
 }

interestingly enough, if I disable dynamic.path, the issue goes away but I consider it a workaround, not a solution since I need the dynamic.path in other scenarios.

Powermock version: 1.6.3 | Junit version: 4.11 | Intellij version: 2016.3

I'll appreciate any insights.

like image 795
Vitaliy Avatar asked Dec 26 '16 20:12

Vitaliy


2 Answers

Turned out this is a known issue The workaround was to set property idea.dynamic.classpath.jar in IDEA_CONFIG/options/options.xml to true.

Note that it was set to false in the first place due to another know issue.

This is pretty cumbersome but this is the best there is for now (March 2017).

like image 168
Vitaliy Avatar answered Sep 30 '22 00:09

Vitaliy


For me it worked by changing the test configuration setup shorten command line. I changed it from classpath file to JAR manifest. It's located in Run -> Edit Configurations. Just make sure you change the correct configuration for the test you are running.

enter image description here

like image 23
Slava Shpitalny Avatar answered Sep 30 '22 00:09

Slava Shpitalny