Zakir Hossain
09/13/2022, 7:15 AM2022-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
@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();
}
@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.uglyog
uglyog
uglyog
Zakir Hossain
09/13/2022, 7:26 AMuglyog
Timothy Jones
09/13/2022, 7:27 AMZakir Hossain
09/13/2022, 7:28 AM@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"));
}
uglyog
uglyog
context.setTarget(new HttpTestTarget("localhost", <port>));
Zakir Hossain
09/13/2022, 7:30 AM@SpringBootTest(
classes = TestWebSecurityConfig.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
That's why I didn't use any portuglyog
@LocalServerPort
annotationZakir Hossain
09/13/2022, 7:33 AMZakir Hossain
09/13/2022, 7:35 AMZakir Hossain
09/13/2022, 7:36 AMZakir Hossain
09/13/2022, 7:36 AM