为了简化单元测试,我创建了以下父类:
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() }
}
}
还有一个单元测试孩子,如下所示:
@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)
}
}
}
}
但是,当我运行它时,出现以下异常:
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)
答案 0 :(得分:1)
您遇到了 Java 的 Generic Type Erasure 的问题。
当 Mockito 创建并注入模拟时,service
实际上看起来像是简单的Object
或Any
:
protected lateinit var service: Any
因此,模拟本身是作为Object
的模拟创建的。
相反,我建议您在实际的测试类中包含与
internal abstract class TestUnitController<CONTROLLER: Any, SERVICE: Any> {
protected abstract val service: SERVICE
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() }
}
}
@RunWith(MockitoJUnitRunner::class)
internal class TestUnitCreateCompanyController : TestUnitController<CreateCompanyController, CompanyService>() {
@Mock
override lateinit var service: CompanyService
@InjectMocks
private lateinit var controller: CreateCompanyController
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)
}
}
}
}