2013-04-30 117 views
0

只需添加以下依賴於我的Android Maven項目:PowerMock ClassCastException異常試圖與PowerMockRunner運行測試時

 <dependency> 
      <groupId>org.powermock</groupId> 
      <artifactId>powermock-module-junit4</artifactId> 
      <version>${powermock.version}</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.powermock</groupId> 
      <artifactId>powermock-api-mockito</artifactId> 
      <version>${powermock.version}</version> 
      <scope>test</scope> 
     </dependency> 

然後用註釋@RunsWith(PowerMockRunner.class)類

當執行MVN乾淨的測試,我得到這個怪異的類轉換異常:

java.lang.ClassCastException: org.powermock.modules.junit4.internal.impl.PowerMockJUnit49RunnerDelegateImpl cannot be cast to org.powermock.modules.junit4.common.internal.PowerMockJUnitRunnerDelegate 
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:143) 
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:39) 
    at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:217) 
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:59) 
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:32) 
    at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:33) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31) 
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24) 
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57) 
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29) 
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57) 
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24) 
    at org.apache.maven.surefire.junit4.JUnit4TestSet.<init>(JUnit4TestSet.java:45) 
    at org.apache.maven.surefire.junit4.JUnit4DirectoryTestSuite.createTestSet(JUnit4DirectoryTestSuite.java:56) 
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.locateTestSets(AbstractDirectoryTestSuite.java:96) 
    at org.apache.maven.surefire.Surefire.createSuiteFromDefinition(Surefire.java:209) 
    at org.apache.maven.surefire.Surefire.run(Surefire.java:156) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338) 
    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997) 

我發現,是由於在同一個測試項目的測試robolectric runne使用河但它不適用於相同的測試。我的意思是,有@RunsWith(RobolectricTestRunner.class)和@RunsWith(PowerMockTestRunner.class)的新類。刪除所有使用Robolectric註解的類時,它都可以工作。

任何想法爲什麼會發生這種情況?

在此先感謝

+0

請問你的類在測試下有android依賴嗎?所以或者它使用一些直接或父母嘗試實例化或調用某些事件或事件提及的東西? – 2013-05-08 04:50:42

回答

0

通過自定義的類裝載器都RobolectricPowerMock儀器代碼。一些可能的解決方法:

  1. 爲PowerMock測試創建一個新的Maven子模塊。

  2. 我還沒有測試過這個,但是你可能會使用annotation @PowerMockIgnore來使它工作。

  3. Change the fork mode在surefire-plugin中爲pertest。我沒有嘗試過,因爲它可能會減慢測試速度。

相關問題