0

有没有人遇到过@MockkBean似乎没有实际工作或至少存根没有通过的问题?

简单的例子:

@RunWith(SpringRunner::class)
@WebMvcTest(controllers = [WidgetController::class])
class WidgetTest {

    @Autowired
    private lateinit var mockMvc: MockMvc
    @MockkBean
    private lateinit var widgetService: WidgetService

    @Test
    fun test() {
        val value = objectMapper.readValue<MyWidget>(something()))
        every {
            widgetService.getWidget(ArgumentMatchers.anyString())
        } returns value


        mockMvc
                .perform(
                        get(
                                "/apis/widget/v1/widget"
                        ).contentType(MediaType.APPLICATION_JSON)
                )
                .andExpect {
                    JSONAssert.assertEquals(
                           widgetPayload(),
                            it.response.contentAsString,
                            false
                    )
                }
    }
}

结果是:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is io.mockk.MockKException: no answer found for: WidgetService(com.bondhouse.pms.services.externalportfolios.ExternalPortfoliosService#0 bean#1).getWidget(test)
4

1 回答 1

1

总是在发帖后 3 分钟找出答案...

确保您没有导入 Mockito 参数匹配器ArgumentMatcher.any()

于 2019-09-20T00:26:42.213 回答