2016-02-24 91 views
1

當我嘗試在Android Studio中運行我的單元測試時出現運行時錯誤,而我似乎無法解決。我猜這是與依賴關係,因爲錯誤跟蹤沒有指定我的任何單元測試代碼。在Android Studio上使用Powermock(使用mockito)執行簡單單元測試時發生運行時異常

我app.gradle文件看起來像:

dependencies { 
    testCompile 'org.mockito:mockito-core:1.10.19' 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:23.0.1' 
    testCompile 'junit:junit:4.12' 
    testCompile 'org.powermock:powermock-api-mockito:1.6.3' 
    testCompile 'org.powermock:powermock-module-junit4-rule-agent:1.6.3' 
    testCompile 'org.powermock:powermock-module-junit4-rule:1.6.3' 
    testCompile 'org.powermock:powermock-module-junit4:1.6.3' } 

錯誤我得到的樣子:

java.lang.RuntimeException: Invoking the beforeTestMethod method on PowerMock test listener [email protected] failed. 
at org.powermock.tests.utils.impl.PowerMockTestNotifierImpl.notifyBeforeTestMethod(PowerMockTestNotifierImpl.java:92) 
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:292) 
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127) 
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82) 
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282) 
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87) 
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50) 
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207) 
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146) 
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120) 
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34) 
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44) 
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122) 
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106) 
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53) 
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78) 
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212) 
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68) 
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) 


Caused by: java.lang.IllegalArgumentException: Cannot subclass final class class [[I 
at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:447) 
at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) 
at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217) 
at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378) 
at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318) 
at org.powermock.api.mockito.repackaged.ClassImposterizer.createProxyClass(ClassImposterizer.java:123) 
at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:57) 
at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:111) 
at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:59) 
at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:203) 
at org.powermock.api.extension.listener.AnnotationEnabler.standardInject(AnnotationEnabler.java:106) 
at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:54) 
at org.powermock.tests.utils.impl.PowerMockTestNotifierImpl.notifyBeforeTestMethod(PowerMockTestNotifierImpl.java:90) 
... 24 more 


Process finished with exit code 254  

我同時使用:

@RunWith(PowerMockRunner.class) 
@PrepareForTest(Projectile.class) 

任何幫助將是感謝,謝謝!

回答

3

你能否提供更多關於被測試的類的信息和關於自我測試的信息。

根據日誌,PowerMock無法爲測試類的字段之一創建模擬對象,該字段由org.mockito.Mock註釋。

java.lang.IllegalArgumentException異常:不能繼承final類類[我

在此基礎上線,類不能被創建,因爲類是final

相關問題