test: Wiremock extension
This commit is contained in:
parent
497574b618
commit
b2cbfad2ac
2 changed files with 52 additions and 0 deletions
7
pom.xml
7
pom.xml
|
@ -27,6 +27,7 @@
|
|||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
|
||||
<!-- dependencies -->
|
||||
<wiremock.version>3.5.4</wiremock.version>
|
||||
|
||||
<!-- plugins -->
|
||||
<jsonschema2pojo.version>1.2.1</jsonschema2pojo.version>
|
||||
|
@ -78,6 +79,12 @@
|
|||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wiremock</groupId>
|
||||
<artifactId>wiremock</artifactId>
|
||||
<version>${wiremock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
45
src/test/java/eu/ztsh/wymiana/WireMockExtension.java
Normal file
45
src/test/java/eu/ztsh/wymiana/WireMockExtension.java
Normal file
|
@ -0,0 +1,45 @@
|
|||
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).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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Throwable {
|
||||
wireMockServer.stop();
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue