This Quarkus JUnit5 MockK extension allows you to easily inject MockK mocks.
The full documentation be found here.
First of all, you need to add the following dependency:
<dependency>
<groupId>io.quarkiverse.mockk</groupId>
<artifactId>quarkus-junit5-mockk</artifactId>
<version>LATEST</version>
<scope>test</scope>
</dependency>If you are using gradle:
dependencies {
testImplementation 'io.quarkiverse.mockk:quarkus-junit5-mockk:LATEST'
}Starting with version 3.8+, version 3+ of quarkus-junit5-mockk should be used.
If you use a version between 3.0.0, and before 3.8.0, version 2.0.0 of quarkus-junit5-mockk should be used.
If you use a version between 2.8 and before 3.0.0, version 1.1.0 of quarkus-junit5-mockk should be used.
If you use a version before 2.7, version 1.0.1 of quarkus-junit5-mockk should be used.
Now, you can use @InjectMock and @InjectSpy in your test such as:
@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() }
}
}