The contract-test uses a random port on the provider
authorKai Moritz <kai@juplo.de>
Sun, 22 May 2022 07:19:09 +0000 (09:19 +0200)
committerKai Moritz <kai@juplo.de>
Fri, 3 Jun 2022 11:50:24 +0000 (13:50 +0200)
* This is crucial in most cases, if one planes to execute the test in
  a build pipline, because it is very likely, that several tests run in
  parallel in that scenario

provider/src/test/java/io/pactflow/example/sirenprovider/SirenProviderApplicationTests.java

index 1f5e96a..9bf6475 100644 (file)
@@ -1,21 +1,34 @@
 package io.pactflow.example.sirenprovider;
 
+import au.com.dius.pact.provider.junit5.HttpTestTarget;
 import au.com.dius.pact.provider.junit5.PactVerificationContext;
 import au.com.dius.pact.provider.junitsupport.Provider;
 import au.com.dius.pact.provider.junitsupport.loader.PactFolder;
 import au.com.dius.pact.provider.spring.junit5.PactVerificationSpringProvider;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.TestTemplate;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.web.server.LocalServerPort;
 
-import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
+import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
 
 
-@SpringBootTest(webEnvironment = DEFINED_PORT)
+@SpringBootTest(webEnvironment = RANDOM_PORT)
 @Provider("SirenOrderProvider")
 @PactFolder("../spring-consumer/target/pacts")
 class SirenProviderApplicationTests
 {
+  @LocalServerPort
+  int port;
+
+
+  @BeforeEach
+  public void init(PactVerificationContext context)
+  {
+    context.setTarget(new HttpTestTarget("localhost", port));
+  }
+
   @TestTemplate
   @ExtendWith(PactVerificationSpringProvider.class)
   public void pactVerificationTestTemplate(PactVerificationContext context)