Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot Kotlin Spring WebMvcTest Mockk_Spring Boot_Testing_Kotlin_Mockk - Fatal编程技术网

Spring boot Kotlin Spring WebMvcTest Mockk

Spring boot Kotlin Spring WebMvcTest Mockk,spring-boot,testing,kotlin,mockk,Spring Boot,Testing,Kotlin,Mockk,有没有人遇到过@mockbean似乎不起作用或者至少存根没有通过的问题 简单的例子是: @RunWith(SpringRunner::class) @WebMvcTest(controllers = [WidgetController::class]) class WidgetTest { @Autowired private lateinit var mockMvc: MockMvc @MockkBean private lateinit var widget

有没有人遇到过@mockbean似乎不起作用或者至少存根没有通过的问题

简单的例子是:

@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
                    )
                }
    }
}

始终在发布后3分钟内找出答案

确保没有使用ArgumentMatcher.any导入Mockito参数匹配器

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)