0
votes

When using Mockito in JUnit4, I encounter the error in the title.

My code is shown below

package cn.patest.judgerAutoscalerKotlin

import org.junit.Test
import org.mockito.*

import java.lang.RuntimeException

class CalculateScaleToTest {
    @Mock
    private lateinit var test: cn.patest.judgerAutoscalerKotlin.Test

    private fun mockTest() {
        test = cn.patest.judgerAutoscalerKotlin.Test()
        Mockito.doAnswer { 1 }.`when`(test).test()
    }


    @Test
    fun z() {
        mockTest()
    }
}
package cn.patest.judgerAutoscalerKotlin

class Test() {
    fun test(): Int {
        return 1
    }
}

Error details are shown below:

org.mockito.exceptions.misusing.NotAMockException: Argument passed to when() is not a mock! Example of correct stubbing: doThrow(new RuntimeException()).when(mock).someMethod(); at cn.patest.judgerAutoscalerKotlin.CalculateScaleToTest.mockTest(CalculateScaleToTest.kt:53) at cn.patest.judgerAutoscalerKotlin.CalculateScaleToTest.z(CalculateScaleToTest.kt:61) 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.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.junit.runner.JUnitCore.run(JUnitCore.java:137)

I searched with google and found some similar issues but they have different situations from mine.

1
It will be incredibly easier to use mockk, if you are using kotlin. It saved me days - VolodymyrH
I think you are missing the mockito rule: @Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); (added as a field to your test class). - Arcanefoam
@Arcanefoam I added ` @get:Rule var mockitoRule = MockitoJUnit.rule()` as you suggest. After that, I encounter the same error. - A.Chao
You sure this is Java? Set appropriate tags please - pafau k.

1 Answers

0
votes

With:

    test = cn.patest.judgerAutoscalerKotlin.Test()

you create a real object, not a mocked one.

Remove the above line and add:

  @Before
  fun before() {

    MockitoAnnotations.initMocks( this )
  }

Reference:

MockitoAnnotations.initMocks(this) method has to be called to initialize annotated fields.

Note also that mocking the class under test in a real environment doesn't make any sense (in contrast to a MCVE like here).