0
我有一個非常大的項目,並在某些時候開發人員開始注意到,如果他們嘗試在intellij中運行/調試PowerMock驅動的測試,他們會得到以下錯誤:Powermock驅動的測試通過maven運行得很好,但不運行在intellij
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
我將不勝感激。
*「'引起:java.lang.ClassNotFoundException:'」*該錯誤很明顯是由IntelliJ中有關類路徑的配置問題引起的。您可能會錯過PowerMock的運行時依賴項。 –
@TimothyTruckle,通過maven作品運行,關閉動態類路徑作品。這恐怕不是那些愚蠢的「天哪,我忘記了依賴」的例子。 – Vitaliy
@ Vitaliy:*「這不是那種愚蠢的」天哪我忘記了一個依賴「情況」*但這是棧跟蹤告訴我們的。你是否將項目導入爲「maven項目」(IntelliJ稱之爲什麼),並且你是否將IntelliJ項目與maven配置正確同步?在* eclipse *父項目不需要被導入到工作區中,IntelliJ的行爲是否相同? –