【问题标题】:Powermock driven tests run just fine via maven but don't run in intellijPowermock 驱动的测试通过 maven 运行得很好,但不能在 intellij 中运行
【发布时间】:2016-12-26 20:14:55
【问题描述】:

我有一个非常大的项目,在某些时候,开发人员开始注意到,如果他们尝试在 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

我将不胜感激。

【问题讨论】:

  • "Caused by: java.lang.ClassNotFoundException:" 这个错误很明显是由于IntelliJ中关于classpath的配置问题造成的。您可能缺少 PowerMock 的运行时依赖项。
  • @TimothyTruckle,通过 maven 运行,关闭动态类路径工作。恐怕这不是那些愚蠢的“天哪,我忘记了依赖”案例之一。
  • @Vitaliy:“这不是那些愚蠢的“天哪,我忘记了依赖”案例之一”,但这是堆栈跟踪告诉我们的。您是否将项目“作为 maven 项目”导入(IntelliJ 称之为)并且您是否将 IntelliJ 项目与 maven 配置正确同步? eclipse中父项目不需要导入工作区,IntelliJ的行为是否一样?
  • @TimothyTruckle,是的,我有。

标签: java maven unit-testing intellij-idea powermock


【解决方案1】:

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

请注意,由于another know issue,它首先被设置为false。

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

【讨论】:

  • 在哪里可以找到这个 xml?
  • @Teja,我在上面的答案中进行了描述。
  • @Vitaliy 现在我们是 2020 年,我目前拥有 -> IntelliJ IDEA 2019.3.4(社区版)构建 #IC-193.6911.18,构建于 2020 年 3 月 17 日运行时版本:11.0。 6+8-b520.43 x86_64 VM:JetBrains s.r.o macOS 10.15.4 的 OpenJDK 64-Bit Server VM -> 而且我没有看到您提供的链接中提到的任何文件。你能帮我解决这个问题吗?
  • @NitinRamac 看看我的回答
【解决方案2】:

对我来说,它通过更改测试配置设置 shorten command line 起作用。 我将它从classpath file 更改为JAR manifest。 它位于Run -&gt; Edit Configurations。只需确保为正在运行的测试更改正确的配置即可。

【讨论】:

  • 没有看到这个缩短的命令行选项?
  • 也许它在新版本的 IntelliJ 中被移到了其他地方
猜你喜欢
  • 2021-08-13
  • 1970-01-01
  • 1970-01-01
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
  • 2020-03-15
  • 1970-01-01
  • 2017-09-30
相关资源
最近更新 更多