Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotAMockException / How to stub a value in parameterized test in Kotlin?

For the following Kotlin class:

class ProductLogic(

        private val product: Product?

) {

    fun shouldShow(): Boolean {
        if (product == null) {
            return false
        }
        val version = product.version!!
        if (!Utils.isAtLeastVersionX(version.major, version.minor)) {
            return false
        }
        return true
    }

}

I am trying to write a parameterized test in Kotlin:

@RunWith(ParameterizedRobolectricTestRunner::class)
@Config(constants = BuildConfig::class, sdk = [19], packageName = "com.example")
class ProductLogicTest(

        private val product: Product?,
        private val shouldShow: Boolean

) {

    @Before
    fun setUp() {
        // doReturn(VERSION).`when`(product).version // (2) Raises a NotAMockException
    }

    @Test
    fun shouldShow() {
        assertThat(ProductLogic(product).shouldShow(), `is`(shouldShow))
    }

    companion object {
        @JvmStatic
        @Parameters(name = "{index}: {0} => {1}")
        fun data(): Collection<Array<Any?>> {
            val productMock = mock<Product>(Product::class.java)
            doReturn(VERSION).`when`(productMock).version // (1) Is not applied
            return asList(
                       arrayOf(productMock, false),
                       // ...
            )
        }

}

I want to parameterize the value of the Product#version property. When I (1) modify its value in the data() function it is not applied when running test. When I (2) try to modify its value in @Before a NotAMockException is raised:

org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to when() is not a mock!
Example of correct stubbing:
    doThrow(new RuntimeException()).when(mock).someMethod();

Please note that the example is simplified - the real ProductLogic class consists of more parameters which rectify to using a parameterized test.

like image 856
JJD Avatar asked Dec 04 '18 16:12

JJD


1 Answers

Robolectric and Mockito versions:

testImplementation 'org.robolectric:robolectric:4.1'
testImplementation 'org.mockito:mockito-core:2.23.4'

Also, to mock final classes, I created file src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker with content:

mock-maker-inline

Classes to test:

class ProductLogic(private val product: Product?) {
    fun shouldShow(): Boolean {
        if (product == null) {
            return false
        }
        val version = product.version
        return !isAtLeastVersionX(version.minor, version.major)
    }

    private fun isAtLeastVersionX(minor: Int, major: Int): Boolean {
        val v = 5
        return v in minor..major
    }
}

class Product(val version: Version)

class Version(val minor: Int, val major: Int)

Next test code works for me and test is passed:

import org.hamcrest.CoreMatchers.`is`
import org.junit.Assert.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.doReturn
import org.mockito.Mockito.mock
import org.robolectric.ParameterizedRobolectricTestRunner
import org.robolectric.annotation.Config
import java.util.Arrays.asList

@RunWith(ParameterizedRobolectricTestRunner::class)
@Config(sdk = [19], packageName = "com.example")
class ProductLogicTest(private val product: Product,
                       private val shouldShow: Boolean) {

    @Before
    fun setUp() {
        //doReturn(VERSION).`when`(product).version // if uncomment works fine
    }

    @Test
    fun shouldShow() {
        assertThat(ProductLogic(product).shouldShow(), `is`(shouldShow))
    }


    companion object {
        private val VERSION = Version(1, 5)

        @JvmStatic
        @ParameterizedRobolectricTestRunner.Parameters(name = "{index}: {0} => {1}")
        fun data(): Collection<Array<Any?>> {
            val productMock = mock(Product::class.java)
            doReturn(VERSION).`when`(productMock).version // Works fine
            return asList(
                    arrayOf(productMock, false)
            )
        }
    }
}
like image 52
Sergey Avatar answered Oct 24 '22 18:10

Sergey