私はjunit、mokito、powermockを一緒にセットアップしようとしますが、テストを実行するとClassNotFoundExceptionが得られます:(
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.7.22'
androidTestCompile 'org.mockito:mockito-core:2.7.22'
androidTestCompile "org.mockito:mockito-Android:2.7.22"
testCompile 'org.robolectric:robolectric:3.3.2'
testCompile 'org.hamcrest:hamcrest-core:1.3'
testCompile 'org.powermock:powermock-core:1.6.6'
testCompile 'org.powermock:powermock-module-junit4:1.6.6'
testCompile 'org.powermock:powermock-module-junit4-rule:1.6.6'
testCompile 'org.powermock:powermock-api-mockito:1.6.6'
testCompile 'org.powermock:powermock-classloading-xstream:1.6.6'`
私も追加してcglibを追加しようとしました:
testCompile 'cglib:cglib:3.1'
testCompile 'cglib:cglib-nodep:3.1'
しかし、不足することなく。
誰もが作業構成を共有したり、何が間違っているかを指摘したりできます。
テストを実行したときの私のスタックトレース:
Exception in thread "main" Java.lang.NoClassDefFoundError: org/mockito/exceptions/Reporter
at Sun.reflect.GeneratedSerializationConstructorAccessor5.newInstance(Unknown Source)
at Java.lang.reflect.Constructor.newInstance(Constructor.Java:423)
at org.objenesis.instantiator.Sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.Java:48)
at org.powermock.reflect.internal.WhiteboxImpl.newInstance(WhiteboxImpl.Java:251)
at org.powermock.reflect.Whitebox.newInstance(Whitebox.Java:139)
at org.powermock.api.extension.reporter.AbstractMockingFrameworkReporterFactory.getInstanceForClassLoader(AbstractMockingFrameworkReporterFactory.Java:41)
at org.powermock.api.extension.reporter.AbstractMockingFrameworkReporterFactory.create(AbstractMockingFrameworkReporterFactory.Java:35)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.getMockingFrameworkReporter(JUnit4TestSuiteChunkerImpl.Java:140)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.Java:119)
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:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.Java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.Java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.Java:84)
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:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.Java:147)
Caused by: Java.lang.ClassNotFoundException: org.mockito.exceptions.Reporter
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)
at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.Java:190)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass1(DeferSupportingClassLoader.Java:77)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.Java:67)
at Java.lang.ClassLoader.loadClass(ClassLoader.Java:357)
... 21 more
それは私のために働いたものです:
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.8.0'
testCompile 'org.powermock:powermock-api-mockito2:1.7.0RC2'
testCompile 'org.powermock:powermock-module-junit4:1.7.0'
testCompile 'org.powermock:powermock-core:1.7.0'
testCompile 'org.powermock:powermock-module-junit4-rule:1.7.0'
mockito2を使用して問題を修正しました。
PowerMock 1.6は、Mockito 2.7と互換性がありません(ご覧ください https://github.com/powermock/powermock/wiki/Mockito#supported-versions )
PowerMock 1.7.0RC2(または1.7.0RC3if1.7.0RC2は機能しません)、またpowermock-api-mockito2powermock-api-mockitoの代わりに。
Kotlin 1.2.61
/mockito 2.15
とpowermock 1.7.0RC2
DID NOTを使用しています。私のためにトリックを行った依存関係のセットは次のとおりです。
dependencies {
testImplementation "junit:junit:4.12"
testImplementation "org.mockito:mockito-core:2.15.0"
testImplementation "io.kotlintest:kotlintest:2.0.7"
testImplementation 'org.powermock:powermock-module-junit4-rule:2.0.0-beta.5'
testImplementation 'org.powermock:powermock-core:2.0.0-beta.5'
testImplementation 'org.powermock:powermock-module-junit4:2.0.0-beta.5'
testImplementation 'org.powermock:powermock-api-mockito2:2.0.0-beta.5'
}
PowerMock 1.7。+(常に最新バージョンを使用することをお勧めします)を使用し、powermock-api-mockitoの代わりにpowermock-api -mockito2を使用する必要があります。
testImplementation "org.powermock:powermock-api-mockito2:${versions.powermock}"