New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
java.lang.NullPointerException using mock() #403
Comments
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. IMPORTANT INFORMATION FOR ANDROID USERS: The regular Byte Buddy mock makers cannot generate code on an Android VM! Java : 0.9
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory; @test Finally the build.gradle file is: android {
} dependencies {
}` |
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
|
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.
The text was updated successfully, but these errors were encountered: