From: Kai Moritz Date: Sun, 22 May 2022 07:19:09 +0000 (+0200) Subject: The contract-test uses a random port on the provider X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=4ba4f8254662399284f9cc5d6bb09e73ad918035;p=demos%2Fexample-siren The contract-test uses a random port on the provider * 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 --- diff --git a/provider/src/test/java/io/pactflow/example/sirenprovider/SirenProviderApplicationTests.java b/provider/src/test/java/io/pactflow/example/sirenprovider/SirenProviderApplicationTests.java index 1f5e96a..9bf6475 100644 --- a/provider/src/test/java/io/pactflow/example/sirenprovider/SirenProviderApplicationTests.java +++ b/provider/src/test/java/io/pactflow/example/sirenprovider/SirenProviderApplicationTests.java @@ -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)