@InjectMock annotation

This annotation allows you to inject MockK mocks in your application.

This annotation has three parameters:

  • relaxed, if set to true, all function will return simple values. Default to false.

  • relaxUnitFun, if set to true, Unit function will be relaxed. Default to false.

  • convertScopes, it set to true, convert the @Singleton scope of bean to @ApplicationScoped. This allows to mock singleton beans

Example

For example, @InjectMock can be used in the following example:

@QuarkusTest
class InjectionMockTest {

    @Inject
    private lateinit var firstService: FirstService

    @InjectMock
    private lateinit var secondService: SecondService

    @Test
    fun `should respond test`() {
        every { secondService.greet() } returns "test"
        assertThat(firstService.greet()).isEqualTo("test")
    }

    @Test
    fun `should respond second`() {
        every { secondService.greet() } returns "second"
        assertThat(firstService.greet()).isEqualTo("second")
        verify { secondService.greet() }
    }
}

In order to mock a RestClient, you must add the @RestClient qualifier alongside the @InjectMock:

@QuarkusTest
class InjectionMockTest {

    @Inject
    private lateinit var firstService: FirstService

    @InjectMock
    @RestClient
    private lateinit var myRestClientService: MyRestClientService

    @Test
    fun `should respond test`() {
        every { myRestClientService.greet() } returns "test"
        assertThat(firstService.greet()).isEqualTo("test")
    }
}