web-dev-qa-db-fra.com

après la mise à niveau vers la classe 2.7 ClassNotFoundException: org.mockito.exceptions.Reporter lors de l'exécution du test

J'essaie de mettre en place Junit, Mokito et Powermock ensemble, mais lorsque je lance un test, j'obtiens 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'`

J'ai aussi essayé d'ajouter cglib en ajoutant:

testCompile 'cglib:cglib:3.1'
testCompile 'cglib:cglib-nodep:3.1'

mais sans manque.

quelqu'un pourrait-il partager la configuration de travail ou me signaler ce qui ne va pas?.

Mon stacktrace quand j'ai fait un test:

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
29
LunaVulpo

C'est ce qui a fonctionné pour moi:

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'

utiliser Mockito2 a corrigé le problème pour moi.

27
SpyZip

PowerMock 1.6 n'est pas compatible avec Mockito 2.7 (jetez un coup d'œil à https://github.com/powermock/powermock/wiki/Mockito#supported-versions )

Vous devez utiliser PowerMock 1.7.0RC2 (ou 1.7.0RC3 si 1.7.0RC2 ne fonctionne pas), et aussi utiliser powermock-api-mockito2 au lieu de powermock-api-mockito .

59
user7605325

J'utilise Kotlin 1.2.61/mockito 2.15 et le powermock 1.7.0RC2N'A PAS FAIT Travaillez-moi. L'ensemble de dépendances qui a fait le tour pour moi est:

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'
}

Vous devez utiliser PowerMock 1.7. + (Il est toujours préférable d’utiliser la dernière version), ainsi que powermock-api -mockito2 au lieu de powermock-api-mockito.

testImplementation "org.powermock:powermock-api-mockito2:${versions.powermock}"
2
SebastienRieu