0

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)
echen
  • 2,002
  • 1
  • 24
  • 38

2 Answers2

1

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

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

echen
  • 2,002
  • 1
  • 24
  • 38
  • If you use IntelliJ, you may exclude mockito **auto importing** in for the project or ide scope. i.e. I exclude old junit packages. There are always naming conflicts between libraries :( IntelliJ IDE's allow disabling auto-import and completition for certain packages in scope of IDE or Project. `IntelliJ > Preferences > Editor > General > Auto Import > Exclude from auto-import and completition` – ocos Mar 19 '22 at 19:01
0

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.

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
SKO
  • 32
  • 5