Skip to content
Snippets Groups Projects
Commit b19af8db authored by Arnaud LABOUREL's avatar Arnaud LABOUREL
Browse files

Added mockwire example

parent b2958c4b
Branches
No related tags found
No related merge requests found
Pipeline #45624 passed
...@@ -19,6 +19,8 @@ dependencies { ...@@ -19,6 +19,8 @@ dependencies {
implementation("com.google.inject:guice:7.0.0") implementation("com.google.inject:guice:7.0.0")
testImplementation("org.mockito:mockito-core:5.15.2") testImplementation("org.mockito:mockito-core:5.15.2")
testImplementation("org.assertj:assertj-core:3.27.2") testImplementation("org.assertj:assertj-core:3.27.2")
testImplementation("org.wiremock:wiremock:3.10.0")
implementation("org.slf4j:slf4j-simple:2.0.16")
} }
application { application {
......
package fr.univ_amu.m1info.service.wiremock;
import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.fail;
public class WireMockTest {
public static final String LOCALHOST = "localhost";
public static final int PORT = 8080;
public static final String PATH = "/my/resource";
public static final String BODY = "\"testing-library\": \"WireMock\"";
@Test
public void exampleTest() {
// create mock server
WireMockServer wireMockServer = new WireMockServer();
// start mock server
wireMockServer.start();
// configure mock server
configureFor(LOCALHOST, PORT);
stubFor(get(PATH)
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(BODY)));
// Setup HTTP GET request (with HTTP Client embedded in Java 11+)
final HttpClient client = HttpClient.newBuilder().build();
try {
final HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("http://"+LOCALHOST+ ":" + PORT + PATH))
.GET().build();
// Send the request and receive the response
final HttpResponse<String> response =
client.send(request, HttpResponse.BodyHandlers.ofString());
// Verify the response (with AssertJ)
assertThat(response.statusCode())
.isEqualTo(200);
assertThat(response.body())
.isEqualTo(BODY);
verify(getRequestedFor(urlEqualTo(PATH)));
assertThat(response.headers().firstValue("Content-Type")).isPresent()
.hasValue("application/json");
}
catch (Exception e){
fail("Exception thrown");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment