@InjectMock annotation
This annotation allows you to inject MockK mocks in your application.
This annotation has three parameters:
-
relaxed, if set totrue, all function will return simple values. Default tofalse. -
relaxUnitFun, if set totrue,Unitfunction will be relaxed. Default tofalse. -
convertScopes, it set totrue, convert the@Singletonscope 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")
}
}