34

I try to set up junit, mokito and powermock together but when I ran a test I get 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'`

I also tried adding cglib by adding:

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

but without of lack.

could any one share working configuration or point me out what is wrong.

My stacktrace when I ran a 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
LunaVulpo
  • 3,043
  • 5
  • 30
  • 58

4 Answers4

64

PowerMock 1.6 isn't compatible with Mockito 2.7 (take a look at https://github.com/powermock/powermock/wiki/Mockito#supported-versions)

You should use PowerMock 1.7.0RC2 (or 1.7.0RC3 if 1.7.0RC2 doesn't work), and also use powermock-api-mockito2 instead of powermock-api-mockito.

galcyurio
  • 1,776
  • 15
  • 25
33

That's what worked for me:

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'

using Mockito2 fixed the problem for me.

SpyZip
  • 5,511
  • 4
  • 32
  • 53
  • There is no mockito-core:2.8.0 available. Can you post the correct version? – KumarAnkit Dec 29 '18 at 06:11
  • 2
    I was able to get it working with this combination: `testCompile 'junit:junit:4.12'` . `testCompile 'org.mockito:mockito-core:2.9.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'` . – yokeho Jan 30 '19 at 00:33
  • use `testImplementation` instead of `testCompile` as it has been deprecated – Mario Codes Jun 04 '20 at 10:00
3

I'm using Kotlin 1.2.61/mockito 2.15 and powermock 1.7.0RC2 DID NOT work me. The set of dependencies that did the trick for me is:

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

You should use PowerMock 1.7.+ (it's always better to use the last version), and also use powermock-api-mockito2 instead of powermock-api-mockito.

testImplementation "org.powermock:powermock-api-mockito2:${versions.powermock}"
SebastienRieu
  • 1,443
  • 2
  • 10
  • 20