使用 MockK 模拟 Spring Repository 时,类 java.lang.Object 不能强制转换为类 Task

Posted

技术标签:

【中文标题】使用 MockK 模拟 Spring Repository 时,类 java.lang.Object 不能强制转换为类 Task【英文标题】:class java.lang.Object cannot be cast to class Task when mocking Spring Repository with MockK 【发布时间】:2021-12-13 03:22:39 【问题描述】:

我的 Task 实体有一个非常简单的 Spring 存储库单元测试。

这是存储库的配置方式:

@Repository
interface TaskRepository : CrudRepository<Task, Long>

这是我要测试的逻辑:

// TaskService.kt
    fun deleteSubTask(parentTask: Task, subTask: Task) 
        taskRepository.save(parentTask)
        parentTask.subTasks?.remove(subTask)
        taskRepository.delete(subTask)
    

这是我的单元测试(使用 JUnit5 和 Mockk):

//TaskServiceTest.kt

internal class TaskServiceTest 
    private val taskRepository: TaskRepository = mockk(relaxed = true)
    private val taskService = TaskService(mockk(relaxed = true), taskRepository)

    @Test
    fun `should remove subtask of parent task`() 
        val subTask: Task = mockk()
        val parentTask: Task = mockk()

        taskService.deleteSubTask(parentTask, subTask)

        verify  parentTask.subTasks?.remove(subTask) 
        verify  taskRepository.delete(subTask) 
        verify  taskRepository.save(parentTask) 
    

执行测试时出现以下错误:

22:48:51.245 [Test worker] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for TaskRepository name=#1
22:48:51.483 [Test worker] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for UserRepository name=#2
22:48:51.508 [Test worker] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for Task name=#3
22:48:51.552 [Test worker] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for Task name=#4
22:48:51.853 [Test worker] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for Any name=child of #1#5
22:48:51.888 [Test worker] DEBUG io.mockk.impl.recording.states.AnsweringState - Answering Any(child of #1#5) on TaskRepository(#1).save(Task(#4))

class java.lang.Object cannot be cast to class com.imhotep.backend.domain.model.dao.Task (java.lang.Object is in module java.base of loader 'bootstrap'; com.imhotep.backend.domain.model.dao.Task is in unnamed module of loader 'app')
java.lang.ClassCastException: class java.lang.Object cannot be cast to class com.imhotep.backend.domain.model.dao.Task (java.lang.Object is in module java.base of loader 'bootstrap'; com.imhotep.backend.domain.model.dao.Task is in unnamed module of loader 'app')
    at com.imhotep.backend.repository.TaskRepository$Subclass0.save(Unknown Source)
    at com.imhotep.backend.repository.TaskRepository$Subclass0.save(Unknown Source)
    at com.imhotep.backend.service.TaskService.deleteSubTask(TaskService.kt:54)
    at com.imhotep.backend.service.TaskServiceTest.should remove subtask of parent task(TaskServiceTest.kt:18)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
    at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
    at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
    at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:210)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:206)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:131)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:65)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)
    at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:99)
    at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.access$000(JUnitPlatformTestClassProcessor.java:79)
    at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.stop(JUnitPlatformTestClassProcessor.java:75)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:61)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
    at jdk.proxy1/jdk.proxy1.$Proxy2.stop(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.stop(TestWorker.java:135)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:182)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:164)
    at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:414)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
    at java.base/java.lang.Thread.run(Thread.java:831)

我不知道为什么taskRepository.delete(subTask) 确实有效,而对于taskRepository.save(parentTask),测试失败并显示错误?

【问题讨论】:

您通常不应该模拟数据值,而应该只模拟具有行为的对象。您的 Task 对象被模拟是不正常的;它们应该是真正的 POJO(POKO?)。 我可以说Task 具有删除subTasks 元素的“行为”。这就是我嘲笑它的原因 - 看看这个方法是否被正确调用 是的,同意@chrylis-cautiouslyoptimistic-我不会编写测试。问题是你需要输入模拟电话val task = mockk&lt;Task&gt;() 我明白你的意思。然而,创建这些参数的简单模拟而不是使用构造函数创建整个实例对我来说似乎更容易,因为我只需要查看它们是否在 TaskRepository 函数中使用。我有点认为这样做可以吗? @DCTID 不幸的是,它不适用于使用mockk 的类型调用。据我所知,在 Kotlin 中,类型推断应该就足够了 【参考方案1】:

似乎引发的错误是因为我将TaskRepository 模拟声明为relaxed。当仅声明 void 函数(那些在 Kotlin 中返回 Unit 的函数)被放宽时,它起作用了。

这是工作单元测试:

internal class TaskServiceTest 
    private val taskRepository: TaskRepository = mockk(relaxUnitFun = true) // changed from relaxed to relaxUnitFun
    private val taskService = TaskService(mockk(relaxed = true), taskRepository)

    @Test
    fun `should remove subtask of parent task`() 
        val subTask: Task = mockk()
        val parentTask: Task = mockk() 
            every  subTasks  returns mutableListOf(subTask)
        

        every  taskRepository.save(any())  returns mockk()

        taskService.deleteSubTask(parentTask, subTask)

        verify  parentTask.subTasks?.remove(subTask) 
        verify  taskRepository.delete(subTask) 
        verify  taskRepository.save(parentTask) 
    

【讨论】:

以上是关于使用 MockK 模拟 Spring Repository 时,类 java.lang.Object 不能强制转换为类 Task的主要内容,如果未能解决你的问题,请参考以下文章

kotlin:模拟注入的单元测试(mockK)

使用多平台模拟 kotlin 中的常见测试

单元测试--mockk

Kotlin Multiplatform:如何在 iOS 的单元测试中模拟对象

MockK没有找到答案:

使用 Kotlin 数据类的 Json 解析器正确返回 json 数据,但是为啥解析器(MockK)的单元测试会失败?