Skip to content

Commit

Permalink
fix(test): Support a changing environment for the MockClientServer in…
Browse files Browse the repository at this point in the history
…jection

I.e. if the devservice is temporarily deactivated, delete the instance and do not try to inject it.
If the hostname and or port have changed, recreate the client with new coordinates.

Fixes #177
  • Loading branch information
edeweerd1A committed Aug 30, 2024
1 parent 2e90c0c commit 4fb0c61
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
Expand Down Expand Up @@ -43,4 +44,10 @@ public static class Data {
public String key;
public String value;
}

@GET
@Path("3")
public Response anotherEndpoint() {
return Response.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.quarkiverse.mockserver.it.base;

import static io.restassured.RestAssured.given;

import java.util.Map;

import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;

@QuarkusTest
@TestProfile(TestNotUsingMockServerDevService.TestProfile.class)
public class TestNotUsingMockServerDevService {
@Test
public void test200() {
given()
.contentType(MediaType.APPLICATION_JSON)
.get("/test/3")
.prettyPeek()
.then()
.statusCode(Response.Status.OK.getStatusCode());
}

public static class TestProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of("quarkus.mockserver.devservices.enabled", "false");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,25 @@ public void stop() {

@Override
public void inject(TestInjector testInjector) {
testInjector.injectIntoFields(CLIENT,
new TestInjector.AnnotatedAndMatchesType(InjectMockServerClient.class, MockServerClient.class));
if (CLIENT != null) {
testInjector.injectIntoFields(CLIENT,
new TestInjector.AnnotatedAndMatchesType(InjectMockServerClient.class, MockServerClient.class));
}
}

@Override
public void setIntegrationTestContext(DevServicesContext context) {
String host = context.devServicesProperties().get(MockServerConfig.CLIENT_HOST);
String port = context.devServicesProperties().get(MockServerConfig.CLIENT_PORT);
CLIENT = new MockServerClient(host, Integer.parseInt(port));
if (host == null) {
host = "localhost";
}
if (port == null) {
CLIENT = null;
} else if (CLIENT == null ||
!CLIENT.remoteAddress().getHostName().equals(host) ||
CLIENT.remoteAddress().getPort() != Integer.parseInt(port)) {
CLIENT = new MockServerClient(host, Integer.parseInt(port));
}
}
}

0 comments on commit 4fb0c61

Please sign in to comment.