The micronaut-wiremock
makes simple the use of WireMock for testing a
Micronaut application.
To get started, add the following dependency to the project.
testImplementation "com.rapatao.micronaut:micronaut-wiremock:$version"
<dependency>
<groupId>com.rapatao.micronaut</groupId>
<artifactId>micronaut-wiremock</artifactId>
<version>$version</version>
<scope>test</scope>
</dependency>
By default, it will start the WireMockServer using port as 9090
and host as localhost
. You can easily customize it
using the project configuration file (application[-environment].yml
).
wiremock:
enabled: true
port: 9090
host: localhost
When a custom server is required by any reason, you can customize the WireMockServer providing a bean of
type com.github.tomakehurst.wiremock.core.WireMockConfiguration
. It's useful for testing scenarios which requires
different types of extensions
IMPORTANT: The extension ResponseTemplateTransformer
is globally
active by default
The purpose of the library is simplifying the WireMockServer
initialization, so, you can use all the usual methods
provided by WireMock. If required, you can inject an instance of the WireMockServer
and manipulate it directly.
import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import jakarta.inject.Inject
import org.junit.jupiter.api.Test
@MicronautTest
internal class StubTest {
@Inject
lateinit var wireMockServer: WireMockServer
@Test
fun test() {
wireMockServer.stubFor(WireMock.get("/").willReturn(WireMock.aResponse().withBody("ok")))
...
}
}
import com.github.tomakehurst.wiremock.client.WireMock
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import org.junit.jupiter.api.Test
@MicronautTest
internal class Stub2Test {
@Test
fun test() {
WireMock.stubFor(WireMock.get("/").willReturn(WireMock.aResponse().withBody("ok")))
...
}
}
It is possible to move the stub creation from the testing block by using the JUnit 5
and the
annotation @com.rapatao.micronaut.wiremock.junit.StubFor
.
This annotation extends the JUnit, invoking a class to run the stub definition.
The @com.rapatao.micronaut.wiremock.junit.StubFor
has an argument that must be used to define
all com.rapatao.micronaut.wiremock.junit.StubDefinition
that must be configured
before the test execution. It accepts a list, that are invoked in the declared order.
class ExampleStubDefinition : StubDefinition {
override fun configure() {
WireMock.stubFor(
...
)
stubFor(
...
)
}
}
It is possible to declare the annotation in the class
or in the method
, each one works differently, as below:
When declared in a class
, it acts as a @BeforeAll
, invoking the stub definition class before all test methods,
removing the identified mappings after all testes finish.
When declared in a method
, it acts as a @BeforeEach
, invoking the stub definition class before each test method
execution and remove the identified mappings after each execution.
The annotation, when declared at class level, must be after the @MicronautTest
annotation. The JUnit invokes the
extensions using the declared order, so, when the @StubFor
is declared before the @MicronautTest
, the Micronaut
context is not yet initialized, which means, that the WireMock instance is not yet configured.