Hello, I'm trying to run 2 pact tests. However, th...
# pact-jvm
z
Hello, I'm trying to run 2 pact tests. However, they failed with the following exception.
Copy code
2022-09-13 13:09:20.063 ERROR 43371 --- [           main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@63ce874f] to prepare test instance [no.mentordigital.campaigndb.contracts.ITCampaignApiContractCampaignMasterAdmin@41ac1ebf]

java.lang.IllegalStateException: Failed to load ApplicationContext

Caused by: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.boot.web.server.PortInUseException: Port 8080 is already in use
If I remove one test, the other executes successfully. Below are given my tests
Copy code
@Slf4j
@Provider("campaign-manager")
@Consumer("subscription")
@PactBroker(
    url = "<http://0.0.0.0:9292/>",
    authentication = @PactBrokerAuth(token = "a-valid-token"))
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class ITCampaignApiContractSubscription {

  @Autowired ObjectMapper objectMapper;
  @Autowired CampaignRepository campaignRepository;
  @Autowired ProductRepository productRepository;
  @MockBean ClientRegistrationRepository clientRegistrationRepository;

  @BeforeEach
  void before(PactVerificationContext context) {
    context.setTarget(new HttpTestTarget("localhost", 8080));
  }

  @TestTemplate
  @ExtendWith(PactVerificationInvocationContextProvider.class)
  void pactVerificationTestTemplate(PactVerificationContext context) {
    context.verifyInteraction();
  }

  @State("It has a request to get a campaign by slug name")
  public void toStateWithData() {    
    saveCampaignForContractTest();
  }
Copy code
@Slf4j
@Provider("campaign-manager")
@Consumer("campaign-master-admin")
@PactBroker(
    url = "<http://0.0.0.0:9292/>",
    authentication = @PactBrokerAuth(token = "a-valid-token"))
@SpringBootTest(
    classes = TestWebSecurityConfig.class,
    webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class ITCampaignApiContractCampaignMasterAdmin {

  @Autowired ObjectMapper objectMapper;
  @Autowired CampaignRepository campaignRepository;
  @Autowired ProductRepository productRepository;
  @MockBean ClientRegistrationRepository clientRegistrationRepository;

  @BeforeEach
  void before(PactVerificationContext context) {
    context.setTarget(new HttpTestTarget("localhost", 8080));
  }

  @TestTemplate
  @ExtendWith(PactVerificationInvocationContextProvider.class)
  void pactVerificationTestTemplate(PactVerificationContext context) {
    context.verifyInteraction();
  }

  @State("It has a request to get a campaign by id")
  public void toStateWithData() {
    saveCampaignForContractTest();
  }
I tried with different ports, but no luck.
u
This is not a Pact issue. Your test is trying to start the server on a port that is already being used. I assume that your test is not stopping your server correctly (i.e. the first test runs, and uses port 8080, then the second one runs and 8080 is already in use)
You can get each test to use a random port
Or try work out why the server is not shutting down correctly
z
Yes, that is the problem. But 2 pact tests cannot run together. The 1st one doesn't shutdown the server properly.
u
Use a random port then
t
Pact should shutdown the server correctly, unless there's something misconfigured
z
I used the random port like below, but didn't work
Copy code
@Provider("campaign-manager")
@Consumer("campaign-master-admin")
@PactBroker(
    url = "<http://0.0.0.0:9292/>",
    authentication = @PactBrokerAuth(token = ""))
@SpringBootTest(
    classes = TestWebSecurityConfig.class,
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ITCampaignApiContractCampaignMasterAdmin {

  @Autowired ObjectMapper objectMapper;
  @Autowired CampaignRepository campaignRepository;
  @Autowired ProductRepository productRepository;
  @MockBean ClientRegistrationRepository clientRegistrationRepository;

  @BeforeEach
  void before(PactVerificationContext context) {
    context.setTarget(new HttpTestTarget("localhost"));
  }
u
This is a Spring test. Spring manages the test context.
You need to set the port here:
Copy code
context.setTarget(new HttpTestTarget("localhost", <port>));
z
I used random port
Copy code
@SpringBootTest(
    classes = TestWebSecurityConfig.class,
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
That's why I didn't use any port
u
You can inject it into the test using the
Copy code
@LocalServerPort
annotation
🙌 1
z
Ok, trying
It works !!
Thanks a lot 👍
👍 2
❤️