Skip to content

Commit 7fc04b1

Browse files
authored
Prefer to use HttpServer and GrpcServer to retrieve running ports instead of configuration (#2070)
1 parent a1cbc98 commit 7fc04b1

6 files changed

Lines changed: 23 additions & 32 deletions

File tree

grpc-locations/src/test/kotlin/io/quarkus/sample/superheroes/location/ContractVerificationTests.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import au.com.dius.pact.provider.junitsupport.loader.PactFolder
1010
import au.com.dius.pact.provider.junitsupport.loader.SelectorBuilder
1111
import io.mockk.every
1212
import io.quarkiverse.test.junit.mockk.InjectSpy
13+
import io.quarkus.grpc.runtime.GrpcServer
1314
import io.quarkus.sample.superheroes.location.repository.LocationRepository
1415
import io.quarkus.test.junit.QuarkusTest
15-
import org.eclipse.microprofile.config.inject.ConfigProperty
1616
import org.junit.jupiter.api.BeforeEach
1717
import org.junit.jupiter.api.TestTemplate
1818
import org.junit.jupiter.api.extension.ExtendWith
@@ -24,7 +24,7 @@ import org.junit.jupiter.api.extension.ExtendWith
2424
// if you'd like to use a Pact broker. You'd also un-comment the following 2 annotations
2525
//@PactBroker(url = "https://quarkus-super-heroes.pactflow.io")
2626
//@EnabledIfSystemProperty(named = "pactbroker.auth.token", matches = ".+", disabledReason = "pactbroker.auth.token system property not set")
27-
class ContractVerificationTests(@ConfigProperty(name = "quarkus.grpc.server.test-port") val quarkusPort: Int) {
27+
class ContractVerificationTests() {
2828
@InjectSpy
2929
lateinit var locationRepository: LocationRepository
3030

@@ -44,10 +44,10 @@ class ContractVerificationTests(@ConfigProperty(name = "quarkus.grpc.server.test
4444
}
4545

4646
@BeforeEach
47-
fun beforeEach(context: PactVerificationContext) {
47+
fun beforeEach(context: PactVerificationContext, grpcServer: GrpcServer) {
4848
context.target = PluginTestTarget(mutableMapOf(
4949
"host" to "localhost",
50-
"port" to quarkusPort,
50+
"port" to grpcServer.port,
5151
"transport" to "grpc"
5252
))
5353

grpc-locations/src/test/kotlin/io/quarkus/sample/superheroes/location/grpc/LocationGrpcServiceIT.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import io.grpc.ManagedChannel
44
import io.grpc.ManagedChannelBuilder
55
import io.grpc.Status.Code
66
import io.grpc.StatusRuntimeException
7+
import io.quarkus.grpc.runtime.GrpcServer
78
import io.quarkus.test.junit.QuarkusIntegrationTest
89
import io.quarkus.sample.superheroes.location.Location
910
import io.quarkus.sample.superheroes.location.LocationType
1011
import io.quarkus.sample.superheroes.location.grpc.LocationsGrpc.LocationsBlockingStub
1112
import io.quarkus.sample.superheroes.location.mapping.LocationMapper
1213
import org.assertj.core.api.Assertions.assertThat
1314
import org.assertj.core.api.Assertions.assertThatThrownBy
14-
import org.eclipse.microprofile.config.ConfigProvider
1515
import org.junit.jupiter.api.AfterAll
1616
import org.junit.jupiter.api.BeforeAll
1717
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation
@@ -49,9 +49,8 @@ class LocationGrpcServiceIT {
4949

5050
@BeforeAll
5151
@JvmStatic
52-
internal fun beforeAll() {
53-
val port = ConfigProvider.getConfig().getValue("quarkus.http.test-port", Integer::class.java)
54-
channel = ManagedChannelBuilder.forAddress("localhost", port.toInt())
52+
internal fun beforeAll(grpcserver: GrpcServer) {
53+
channel = ManagedChannelBuilder.forAddress("localhost", grpcserver.port)
5554
.usePlaintext()
5655
.build()
5756

rest-fights/src/test/java/io/quarkus/sample/superheroes/fight/ContractVerificationTests.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import java.util.List;
66
import java.util.Optional;
77

8-
import org.eclipse.microprofile.config.inject.ConfigProperty;
8+
import io.quarkus.vertx.http.HttpServer;
9+
910
import org.junit.jupiter.api.BeforeEach;
1011
import org.junit.jupiter.api.TestTemplate;
1112
import org.junit.jupiter.api.extension.ExtendWith;
@@ -33,18 +34,15 @@
3334
public class ContractVerificationTests {
3435
private static final String NO_FIGHTS_FOUND_STATE = "No fights exist";
3536

36-
@ConfigProperty(name = "quarkus.http.test-port")
37-
int quarkusPort;
38-
3937
@TestTemplate
4038
@ExtendWith(PactVerificationInvocationContextProvider.class)
4139
void pactVerificationTestTemplate(PactVerificationContext context) {
4240
context.verifyInteraction();
4341
}
4442

4543
@BeforeEach
46-
void beforeEach(PactVerificationContext context) {
47-
context.setTarget(new HttpTestTarget("localhost", this.quarkusPort));
44+
void beforeEach(PactVerificationContext context, HttpServer httpServer) {
45+
context.setTarget(new HttpTestTarget("localhost", httpServer.getPort()));
4846

4947
// Have to do this here because the CDI context doesn't seem to be available
5048
// in the @State method below

rest-heroes/src/test/java/io/quarkus/sample/superheroes/hero/ContractVerificationTests.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import java.util.List;
66
import java.util.Optional;
77

8-
import org.eclipse.microprofile.config.inject.ConfigProperty;
8+
import io.quarkus.vertx.http.HttpServer;
9+
910
import org.junit.jupiter.api.BeforeEach;
1011
import org.junit.jupiter.api.TestTemplate;
1112
import org.junit.jupiter.api.extension.ExtendWith;
@@ -35,9 +36,6 @@
3536
public class ContractVerificationTests {
3637
private static final String NO_RANDOM_HERO_FOUND_STATE = "No random hero found";
3738

38-
@ConfigProperty(name = "quarkus.http.test-port")
39-
int quarkusPort;
40-
4139
@InjectSpy
4240
HeroRepository heroRepository;
4341

@@ -48,8 +46,8 @@ void pactVerificationTestTemplate(PactVerificationContext context) {
4846
}
4947

5048
@BeforeEach
51-
void beforeEach(PactVerificationContext context) {
52-
context.setTarget(new HttpTestTarget("localhost", this.quarkusPort));
49+
void beforeEach(PactVerificationContext context, HttpServer httpServer) {
50+
context.setTarget(new HttpTestTarget("localhost", httpServer.getPort()));
5351

5452
// Have to do this here because the CDI context doesn't seem to be available
5553
// in the @State method below

rest-narration/src/test/java/io/quarkus/sample/superheroes/narration/ContractVerificationTests.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.quarkus.sample.superheroes.narration;
22

3-
import org.eclipse.microprofile.config.inject.ConfigProperty;
3+
import io.quarkus.vertx.http.HttpServer;
4+
45
import org.junit.jupiter.api.BeforeEach;
56
import org.junit.jupiter.api.TestTemplate;
67
import org.junit.jupiter.api.extension.ExtendWith;
@@ -23,18 +24,15 @@
2324
//@PactBroker(url = "https://quarkus-super-heroes.pactflow.io")
2425
//@EnabledIfSystemProperty(named = "pactbroker.auth.token", matches = ".+", disabledReason = "pactbroker.auth.token system property not set")
2526
public class ContractVerificationTests {
26-
@ConfigProperty(name = "quarkus.http.test-port")
27-
int quarkusPort;
28-
2927
@TestTemplate
3028
@ExtendWith(PactVerificationInvocationContextProvider.class)
3129
void pactVerificationTestTemplate(PactVerificationContext context) {
3230
context.verifyInteraction();
3331
}
3432

3533
@BeforeEach
36-
void beforeEach(PactVerificationContext context) {
37-
context.setTarget(new HttpTestTarget("localhost", this.quarkusPort));
34+
void beforeEach(PactVerificationContext context, HttpServer httpServer) {
35+
context.setTarget(new HttpTestTarget("localhost", httpServer.getPort()));
3836
}
3937

4038
@PactBrokerConsumerVersionSelectors

rest-villains/src/test/java/io/quarkus/sample/superheroes/villain/ContractVerificationTests.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import java.util.List;
66
import java.util.Optional;
77

8-
import org.eclipse.microprofile.config.inject.ConfigProperty;
8+
import io.quarkus.vertx.http.HttpServer;
9+
910
import org.junit.jupiter.api.BeforeEach;
1011
import org.junit.jupiter.api.TestTemplate;
1112
import org.junit.jupiter.api.extension.ExtendWith;
@@ -32,18 +33,15 @@
3233
public class ContractVerificationTests {
3334
private static final String NO_RANDOM_VILLAIN_FOUND_STATE = "No random villain found";
3435

35-
@ConfigProperty(name = "quarkus.http.test-port")
36-
int quarkusPort;
37-
3836
@TestTemplate
3937
@ExtendWith(PactVerificationInvocationContextProvider.class)
4038
void pactVerificationTestTemplate(PactVerificationContext context) {
4139
context.verifyInteraction();
4240
}
4341

4442
@BeforeEach
45-
void beforeEach(PactVerificationContext context) {
46-
context.setTarget(new HttpTestTarget("localhost", this.quarkusPort));
43+
void beforeEach(PactVerificationContext context, HttpServer httpServer) {
44+
context.setTarget(new HttpTestTarget("localhost", httpServer.getPort()));
4745

4846
// Have to do this here because the CDI context doesn't seem to be available
4947
// in the @State method below

0 commit comments

Comments
 (0)