Skip to content

Commit 2a79e28

Browse files
fix quarkus tests (#18432)
* fix quarkus tests * fix quarkus tests
1 parent 18fb857 commit 2a79e28

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

quarkus-modules/consume-rest-api/consume-api/src/main/java/com/baeldung/javahttpclient/JavaHttpClientPostService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ public class JavaHttpClientPostService {
1919

2020
private final HttpClient httpClient;
2121
private final ObjectMapper objectMapper;
22+
private final String baseUrl;
2223

2324
public JavaHttpClientPostService() {
2425
this.httpClient = HttpClient.newHttpClient();
2526
this.objectMapper = new ObjectMapper();
27+
this.baseUrl = System.getProperty("quarkus.rest-client.post-rest-client.url", "http://localhost:8080"); // default if not overridden
2628
}
2729

2830
public List<Post> getPosts() {
2931
HttpRequest request = HttpRequest.newBuilder()
30-
.uri(URI.create("http://localhost:8080/posts"))
32+
.uri(URI.create(baseUrl + "/posts"))
3133
.GET()
3234
.build();
3335

quarkus-modules/consume-rest-api/consume-api/src/test/java/com/baeldung/javahttpclient/JavaHttpClientPostServiceIntegrationTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,16 @@ public class JavaHttpClientPostServiceIntegrationTest {
3636

3737
@BeforeAll
3838
static void setup() {
39-
// Start WireMock server
40-
wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig()
41-
.port(8080));
39+
// Start WireMock on a random available port
40+
wireMockServer = new WireMockServer(WireMockConfiguration.options().dynamicPort());
4241
wireMockServer.start();
4342

4443
// Configure WireMock to respond to the API endpoint
4544
wireMockServer.stubFor(get(urlEqualTo("/posts")).willReturn(aResponse().withHeader("Content-Type", "application/json")
4645
.withBody("[{\"id\":1,\"title\":\"Post Title 1\",\"description\":\"Post description 1\"}]")));
46+
47+
// Set system property
48+
System.setProperty("quarkus.rest-client.post-rest-client.url", wireMockServer.baseUrl());
4749
}
4850

4951
@AfterAll

quarkus-modules/consume-rest-api/consume-api/src/test/java/com/baeldung/restclient/PostRestClientIntegrationTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ public class PostRestClientIntegrationTest {
3939
@BeforeAll
4040
static void setup() {
4141
// Start WireMock server
42-
wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig()
43-
.port(8080));
42+
wireMockServer = new WireMockServer(WireMockConfiguration.options().dynamicPort());
4443
wireMockServer.start();
4544

4645
// Configure WireMock to respond to the API endpoint
4746
wireMockServer.stubFor(get(urlEqualTo("/posts")).willReturn(aResponse().withHeader("Content-Type", "application/json")
4847
.withBody("[{\"id\":1,\"title\":\"Post Title 1\",\"description\":\"Post description 1\"}]")));
48+
49+
// Set system property
50+
System.setProperty("quarkus.rest-client.post-api.url", wireMockServer.baseUrl());
4951
}
5052

5153
@AfterAll
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
quarkus.http.test-port=8082
1+
quarkus.http.test-port=0

0 commit comments

Comments
 (0)