Skip to content

Commit

Permalink
Output WireMock logs through Slf4j (#27)
Browse files Browse the repository at this point in the history
Fixes #26
  • Loading branch information
maciejwalkowiak committed Feb 14, 2024
1 parent 59f3794 commit 8ce2847
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Objects;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.common.Notifier;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.platform.commons.util.StringUtils;
Expand Down Expand Up @@ -62,7 +63,8 @@ private void resolveOrCreateWireMockServer(ConfigurableApplicationContext contex
// create & start wiremock server
WireMockConfiguration serverOptions = options()
.usingFilesUnderClasspath(resolveStubLocation(options))
.port(options.port());
.port(options.port())
.notifier(new Slf4jNotifier(true));

if (options.extensions().length > 0) {
serverOptions.extensions(options.extensions());
Expand Down Expand Up @@ -130,4 +132,34 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(configuration);
}

// ported from: https://github.com/spring-cloud/spring-cloud-contract/commit/44c634d0e9e82515d2fba66343530eb7d2ba8223
static class Slf4jNotifier implements Notifier {

private static final Logger log = LoggerFactory.getLogger("WireMock");

private final boolean verbose;

Slf4jNotifier(boolean verbose) {
this.verbose = verbose;
}

@Override
public void info(String message) {
if (verbose) {
log.info(message);
}
}

@Override
public void error(String message) {
log.error(message);
}

@Override
public void error(String message, Throwable t) {
log.error(message, t);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package com.maciejwalkowiak.wiremock.spring;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.test.util.TestSocketUtils;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(classes = WireMockConfigurationCustomizerTest.AppConfiguration.class)
Expand All @@ -23,6 +35,7 @@
configurationCustomizers = WireMockConfigurationCustomizerTest.SampleConfigurationCustomizer.class
),
})
@ExtendWith(OutputCaptureExtension.class)
class WireMockConfigurationCustomizerTest {
private static final int USER_SERVICE_PORT = TestSocketUtils.findAvailableTcpPort();
private static final int TODO_SERVICE_PORT = TestSocketUtils.findAvailableTcpPort();
Expand Down Expand Up @@ -56,4 +69,19 @@ void appliesConfigurationCustomizer() {
assertThat(todoService.port()).isEqualTo(TODO_SERVICE_PORT);
}

@Test
void outputsWireMockLogs(CapturedOutput capturedOutput) throws IOException, InterruptedException {
userService.stubFor(get(urlEqualTo("/test"))
.willReturn(aResponse().withHeader("Content-Type", "text/plain").withBody("Hello World!")));

HttpClient httpClient = HttpClient.newHttpClient();
HttpResponse<String> response = httpClient.send(
HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:" + userService.port() + "/test")).build(),
HttpResponse.BodyHandlers.ofString());
assertThat(response.body()).isEqualTo("Hello World!");
assertThat(capturedOutput.getAll())
.as("Must contain debug logging for WireMock")
.contains("Matched response definition:");
}

}

0 comments on commit 8ce2847

Please sign in to comment.