Open omensight opened 3 years ago
"D:\Program Files\Android\Android Studio\jre\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Program Files\Android\Android Studio\lib\idea_rt.jar=62213:D:\Program Files\Android\Android Studio\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Android\Android Studio\lib\idea_rt.jar" com.intellij.rt.execution.CommandLineWrapper C:\Users\mijae\AppData\Local\Temp\idea_classpath1925221024 com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 DataExportBehaviourUnitTest
Not sure why the above is shown, but in the test you've shown you never set up your mock. Could you try this:
class DataExportBehaviourUnitTest {
private val exportBehaviour: ExportBehaviour = mock()
private val sheetList = SheetListFactory.getRandomList(5)
@Test
fun whenExporting_returnTrue() {
whenever(exportBehavior.export).thenReturn(true)
val export = exportBehaviour.export(sheetList)
Assert.assertTrue(export)
}
}
"D:\Program Files\Android\Android Studio\jre\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Program Files\Android\Android Studio\lib\idea_rt.jar=62213:D:\Program Files\Android\Android Studio\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Android\Android Studio\lib\idea_rt.jar" com.intellij.rt.execution.CommandLineWrapper C:\Users\mijae\AppData\Local\Temp\idea_classpath1925221024 com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 DataExportBehaviourUnitTest
Not sure why the above is shown, but in the test you've shown you never set up your mock. Could you try this:
class DataExportBehaviourUnitTest { private val exportBehaviour: ExportBehaviour = mock() private val sheetList = SheetListFactory.getRandomList(5) @Test fun whenExporting_returnTrue() { whenever(exportBehavior.export).thenReturn(true) val export = exportBehaviour.export(sheetList) Assert.assertTrue(export) } }
Thanks for answer. Doing some changes now it gives me another error but don't show me the same message.
This happens again only in androidTest not in test.
I have created another project to reproduce this error and it is exactly the same as in my original project.
When I run the tests it shows me the next error
`java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:74)
at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
at $Proxy4.isTypeMockable(Unknown Source)
at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:238)
at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:226)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:68)
at org.mockito.Mockito.mock(Mockito.java:1895)
at com.omensight.myapplication.ExampleInstrumentedTest.mocking(ExampleInstrumentedTest.kt:52)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:395)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2204)
Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@bdd6cf8
at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:54)
at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:57)
at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:44)
at org.mockito.internal.configuration.plugins.PluginRegistry.
IMPORTANT INFORMATION FOR ANDROID USERS:
The regular Byte Buddy mock makers cannot generate code on an Android VM! To resolve this, please use the 'mockito-android' dependency for your application: http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22mockito-android%22%20g%3A%22org.mockito%22
Java : 0.9 JVM vendor name : The Android Project JVM vendor version : 2.1.0 JVM name : Dalvik JVM version : 0.9 JVM info : null OS name : Linux OS version : 4.14.117-perf-g9f42476
at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<init>(InlineByteBuddyMockMaker.java:171)
at java.lang.Class.newInstance(Native Method)
at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:49)
... 34 more
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory;
at net.bytebuddy.agent.ByteBuddyAgent$ProcessProvider$ForCurrentVm$ForLegacyVm.resolve(ByteBuddyAgent.java:1038)
at net.bytebuddy.agent.ByteBuddyAgent$ProcessProvider$ForCurrentVm.resolve(ByteBuddyAgent.java:1021)
at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:358)
at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:326)
at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:312)
at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker. And the AndroidTest that I use is the next:
@RunWith(MockitoJUnitRunner::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().context
assertEquals("com.omensight.myapplication", appContext.packageName)
}
@Test fun mocking (){ val mock:ISheet = mock() val mock2:ISheet = mock() mock.equals(mock2) verify(mock).equals(any()) } }`
Finally the build.gradle file is: `plugins { id 'com.android.application' id 'kotlin-android' }
android { compileSdkVersion 30 buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.omensight.myapplication"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation ("androidx.arch.core:core-testing:2.1.0")
testImplementation ("com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0")
androidTestImplementation ("com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0")
}`
IMPORTANT INFORMATION FOR ANDROID USERS: The regular Byte Buddy mock makers cannot generate code on an Android VM!
You're missing a dependency:
androidTestImplementation "org.mockito:mockito-android:$mockito_version"
Looks like your trying to using MockMakerInline in Android instrumentation tests. This wont work.
For Android Instrumentation testing I recommend using fakes. Will save you a lot of headaches.
This happens in test (not android test) for me too
"C:\Program Files\Android\Android Studio\jre\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\Android\Android Studio\lib\idea_rt.jar=61748:C:\Program Files\Android\Android Studio\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\User\AppData\Local\Temp\classpath896506186.jar com.intellij.rt.junit.JUnitStarter -ideVersion5 @w@C:\Users\User\AppData\Local\Temp\idea_working_dirs_junit.tmp @C:\Users\User\AppData\Local\Temp\idea_junit.tmp -socket61747
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
Hi, I am working with Mockito and Kotlin, everything works well in AndroidTests, but when I use Mockito on tests, it shows me this message and I don't know how to proceed `"D:\Program Files\Android\Android Studio\jre\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Program Files\Android\Android Studio\lib\idea_rt.jar=62213:D:\Program Files\Android\Android Studio\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Android\Android Studio\lib\idea_rt.jar" com.intellij.rt.execution.CommandLineWrapper C:\Users\mijae\AppData\Local\Temp\idea_classpath1925221024 com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 DataExportBehaviourUnitTest
java.lang.NullPointerException at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:23) at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:238) at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:226) at org.mockito.internal.MockitoCore.mock(MockitoCore.java:68) at org.mockito.Mockito.mock(Mockito.java:1895) at DataExportBehaviourUnitTest.setUp(DataExportBehaviourUnitTest.kt:35) 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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58) 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.CommandLineWrapper.main(CommandLineWrapper.java:64)
Process finished with exit code -1 ` This is the code of the test:
`import com.alphemsoft.education.regression.dataexporter.testfactory.SheetListFactory import com.nhaarman.mockitokotlin2.mock import org.junit.Assert import org.junit.Before import org.junit.Test
class DataExportBehaviourUnitTest { private lateinit var exportBehaviour: ExportBehaviour private val sheetList = SheetListFactory.getRandomList(5)
} `
Help please, I am stuck here.