Project to use Wiremock with Junit5.
All needed to start using the project is to add the dependency to the POM and that's it.
<dependency>
<groupId>com.github.sparkmuse</groupId>
<artifactId>wiremock-junit-jupiter</artifactId>
<version>${version}</version>
</dependency>
or to gradle
compile 'com.github.sparkmuse:wiremock-junit-jupiter:${version}'
- Extend the test class with
@ExtendWith(WiremockExtension.class)
- Annotate the WireMockServer with
@Wiremock
@ExtendWith(WiremockExtension.class)
public class SimpleServerTest {
@Wiremock
private WireMockServer server;
@Test
@DisplayName("simple test")
void posts() {
server.stubFor(get(urlEqualTo("/posts")).willReturn(aResponse().withStatus(200)));
HttpClient client = HttpClient.newBuilder().build();
HttpResponse<String> postsResponse = client.send(HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8080/posts"))
.GET()
.build(), HttpResponse.BodyHandlers.ofString());
assertThat(postsResponse.statusCode()).isEqualTo(200);
}
}
Multiple servers can be set to improve code readability
@ExtendWith(WiremockExtension.class)
public class MultipleServerTest {
@Wiremock(port = 9000)
private WireMockServer postsServer;
@Wiremock(port = 8000)
private WireMockServer statistics;
@Test
@DisplayName("uses multiple wiremock servers to improve readability")
void posts() {
// rest of the test
}
}
If the simple options that come with the @Wiremock annotation feel free to add your own configuration options. The instance will be managed automatically.
@ExtendWith(WiremockExtension.class)
public class InstantiatedOptionsServerTest {
@Wiremock
private final WireMockServer postsServer = new WireMockServer(
WireMockConfiguration.options()
.port(9000)
.containerThreads(20));
@Test
@DisplayName("uses values from instance wiremock server")
void posts() {
// rest of the test
}
}
It supports nested tests.
@ExtendWith(WiremockExtension.class)
class NestedServerTest {
@Wiremock(port = 9000)
private WireMockServer parentSever;
@Test
@DisplayName("some other top level test")
void posts() {
// some other test
}
@Nested
class NestedClass {
@Wiremock(port = 5000)
private WireMockServer nestedServer;
@Test
@DisplayName("uses parent and nested wiremock server")
void posts() throws Exception {
parentSever.stubFor(get(urlEqualTo("/parent")).willReturn(aResponse().withStatus(200)));
nestedServer.stubFor(get(urlEqualTo("/nested")).willReturn(aResponse().withStatus(200)));
HttpClient client = HttpClient.newBuilder().build();
HttpResponse<String> parentResponse = client.send(HttpRequest.newBuilder()
.uri(URI.create("http://localhost:9000/parent"))
.GET()
.build(), HttpResponse.BodyHandlers.ofString());
HttpResponse<String> nestedResponse = client.send(HttpRequest.newBuilder()
.uri(URI.create("http://localhost:5000/nested"))
.GET()
.build(), HttpResponse.BodyHandlers.ofString());
assertThat(parentResponse.statusCode()).isEqualTo(200);
assertThat(nestedResponse.statusCode()).isEqualTo(200);
}
}
}
The example above and more can be found here: Wiremock junit Jupiter examples