echen
echen

Reputation: 2072

Kotlin Spring WebMvcTest Mockk

Has anyone encountered the issue where @MockkBean does not seem to actually work or at least the stubs are not coming through?

The simple example:

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

results in:

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)

Upvotes: 0

Views: 5770

Answers (2)

SKO
SKO

Reputation: 50

My issue was @MockkBean was actually not working as expected(just creating a mock but not injecting it). I dealed with this by manually overriding bean with mockk/spyk in @TestConfiguration.

Upvotes: 0

echen
echen

Reputation: 2072

Always figure out the answer 3 minutes after posting ...

Make sure you are not importing the Mockito argument matcher with ArgumentMatcher.any()

Upvotes: 1

Related Questions