【发布时间】:2020-01-02 10:58:12
【问题描述】:
我有一个这样的测试:
@Test
fun `can execute`() {
whenever(countryRepository.findByIdOrNull("DE")).thenReturn(germany)
underTest.execute()
}
此测试失败并显示以下错误消息:
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
Country cannot be returned by findById()
findById() should return Optional
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
- with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.
很确定这可能是 Mockito 的问题,因为我没有使用 findbyId,而是使用 findByIdOrNull,因为这更适合 kotlin。我不想更改代码来修复测试。
您能帮我解决这个问题或解决这个问题吗?
【问题讨论】:
标签: java unit-testing jpa kotlin mockito