50 lines
1.7 KiB
Java
50 lines
1.7 KiB
Java
package eu.ztsh.wymiana;
|
|
|
|
import com.github.tomakehurst.wiremock.WireMockServer;
|
|
import com.github.tomakehurst.wiremock.client.WireMock;
|
|
import org.junit.jupiter.api.extension.AfterEachCallback;
|
|
import org.junit.jupiter.api.extension.BeforeAllCallback;
|
|
import org.junit.jupiter.api.extension.ExtensionContext;
|
|
|
|
import static com.github.tomakehurst.wiremock.client.WireMock.*;
|
|
|
|
public class WireMockExtension implements BeforeAllCallback, AfterEachCallback, ExtensionContext.Store.CloseableResource {
|
|
|
|
public static final String baseUrl = "http://localhost:38080";
|
|
|
|
public static void response(String endpoint, int status, String body) {
|
|
configureFor(38080);
|
|
stubFor(get(urlEqualTo(endpoint))
|
|
.willReturn(WireMock.status(status)
|
|
.withHeader("Content-Type", "application/json")
|
|
.withBody(body)));
|
|
}
|
|
|
|
public static void verifyGet(int count, String url) {
|
|
verify(exactly(count), getRequestedFor(urlEqualTo(url)));
|
|
}
|
|
|
|
private static final WireMockServer wireMockServer = new WireMockServer(38080);
|
|
private boolean started;
|
|
|
|
@Override
|
|
public void beforeAll(ExtensionContext extensionContext) throws Exception {
|
|
if (!started) {
|
|
wireMockServer.start();
|
|
started = true;
|
|
}
|
|
}
|
|
@Override
|
|
public void afterEach(ExtensionContext extensionContext) throws Exception {
|
|
wireMockServer.listAllStubMappings().getMappings().forEach(wireMockServer::removeStub);
|
|
wireMockServer.findAllUnmatchedRequests().forEach(System.out::println);
|
|
wireMockServer.resetRequests();
|
|
}
|
|
|
|
@Override
|
|
public void close() throws Throwable {
|
|
wireMockServer.stop();
|
|
}
|
|
|
|
|
|
}
|