#StackBounty: #generics #kotlin #mockito Mockito: ClassCastException on generic parent class

Bounty: 50

To abbreviate my unit tests, I created the following parent class:

internal abstract class TestUnitController<CONTROLLER: Any, SERVICE: Any> {

    @Mock
    protected lateinit var service: SERVICE

    @InjectMocks
    protected lateinit var controller: CONTROLLER

    protected abstract fun controllerCall(): CONTROLLER.() -> Unit
    protected abstract fun serviceCall(): SERVICE.() -> Any?

    @Test
    fun `when service returns error should throw ResponseStatusException`() {
        testControllerServiceError(service, serviceCall()) { controllerCall() }
    }

    protected fun <U> testController(value: U, assertions: () -> Unit) {
        testController(service, serviceCall(), value) { assertions() }
    }
}

And a unit test child like the following:

@RunWith(MockitoJUnitRunner::class)
internal class TestUnitCreateCompanyController : TestUnitController<CreateCompanyController, CompanyService>() {

    private val request = CompanyCreateRequest("LOL SARL")

    override fun controllerCall(): CreateCompanyController.() -> Unit = { createCompany(request) }

    override fun serviceCall(): CompanyService.() -> Any? = { createCompany(any()) }

    @Test
    fun `when create company service successful should return created company`() {
        val domain = CompanyDomain(name = request.name)
        testController(domain) {
            controller.createCompany(request).run {
                uuid.shouldEqual(domain.uuid.toString())
                name.shouldEqual(domain.name)
            }
        }
    }
}

However when I run this, I get the following exception:

java.lang.ClassCastException: org.mockito.codegen.Object$MockitoMock$1782227983 cannot be cast to org.m0skit0.fikchador.spring.domain.company.CompanyService

    at org.m0skit0.fikchador.spring.endpoint.company.create.TestUnitCreateCompanyController$serviceCall$1.invoke(TestUnitCreateCompanyController.kt:13)
    at org.m0skit0.fikchador.spring.test.TestUtilsKt.testController(TestUtils.kt:21)
    at org.m0skit0.fikchador.spring.test.TestUnitController.testController(TestUnitController.kt:24)
    at org.m0skit0.fikchador.spring.endpoint.company.create.TestUnitCreateCompanyController.when create company service successful should return created company(TestUnitCreateCompanyController.kt:24)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    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.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.