Powermock 驱动的测试 运行 通过 maven 就可以了,但在 intellij 中就不行 运行

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

我有一个非常大的项目,开发人员在某个时候开始注意到,如果他们尝试 run/debug 在 intellij 中进行 PowerMock 驱动的测试,他们会收到以下错误:

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

我们正在使用 powermockagent,单元测试通常如下所示:

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

    @Rule
    public PowerMockRule rule = new PowerMockRule();

    static {
        PowerMockAgent.initializeIfNeeded();
    }

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

有趣的是,如果我 禁用 dynamic.path,问题就会消失,但我认为这是一种解决方法,而不是解决方案,因为我需要 dynamic.path在其他场景中。

Powermock 版本:1.6.3 | Junit 版本:4.11 | Intellij 版本:2016.3

如有任何见解,我将不胜感激。

结果this is a known issue 解决方法是将 IDEA_CONFIG/options/options.xml 中的 属性 idea.dynamic.classpath.jar 设置为 true.

请注意,由于 another know issue

首先将其设置为 false

这很麻烦,但这是目前最好的(2017 年 3 月)。

对我来说,它通过更改测试配置设置来工作 shorten command line。 我将它从 classpath file 更改为 JAR manifest。 它位于 Run -> Edit Configurations。只需确保为您 运行.

的测试更改正确的配置即可