Skip to content

Commit

Permalink
Merge pull request #178 from edeweerd1A/contrib-support-deactivation-…
Browse files Browse the repository at this point in the history
…one-quarkus-test

fix(test): Support a changing environment for the MockClientServer injection
  • Loading branch information
andrejpetras authored Sep 10, 2024
2 parents f97ac95 + 4fb0c61 commit bd18c04
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 bd18c04

Please sign in to comment.