Open Amarnath9806 opened 3 years ago
Complete Provider test
@RunWith(SpringRestPactRunner.class) @Provider("provider-name") @PactFolder("pacts") @SpringBootTest(webEnvironment = RANDOM_PORT) public class PactVerificationTest {
@TargetRequestFilter public void requestFilter(HttpRequest request) { request.addHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE); } @TestTarget public Target target = new SpringBootHttpTarget();
@State({"person"})
public void getDetails() throws Exception{
target.setRunTimes(1);
// ... mockito mocks
}
}
MockHttpServletRequestBuilder is for when you are using MockMvcTestTarget which uses Spring MockMVC to run the test.
For SpringBootHttpTarget, you need to use a normal HttpRequest object.
Hi @uglyog My complete test class,
@RunWith(SpringRestPactRunner.class) @SpringBootTest(webEnvironment = RANDOM_PORT) @Provider("car-provider") @Consumer("car-service") @PactBroker( host = "localhost:8081", port = "8081", scheme = "http") public class ProviderPactTest {
private static final Logger logger = LoggerFactory.getLogger(ProviderPactTest.class);
@BeforeEach void before(PactVerificationContext context) { System.setProperty("pact.verifier.publishResults", "true");
context.setTarget(new HttpTestTarget("localhost", 8080));
}
@State("Car exists") public void testGetCarOne() throws Exception {
logger.info("Verfying tests");
}
@TestTemplate @ExtendWith(PactVerificationInvocationContextProvider.class) void pactVerificationTestTemplate(PactVerificationContext context) { context.verifyInteraction(); }
This test works fine when executed as a junit but it is throwing below error while executing with gradlew command
org.junit.runners.model.InitializationError
at au.com.dius.pact.provider.junit.InteractionRunner.validate(InteractionRunner.kt:93)
at au.com.dius.pact.provider.junit.InteractionRunner.
Using dependencies au.com.dius:pact-jvm-provider-spring:4.0.10, au.com.dius:pact-jvm-provider-junit5:4.0.10 Plz le me know if this issue is with code or with dependencies
The first thing I can see is you've set Springboot to use a random port, but then telling the Pact target to use port 8080.
Secondly, you need to provide the full stacktrace which will have the cause of the InitializationError
I am getting below error while passing headers in @TargetRequestFilter public void requestFilter(MockHttpServletRequestBuilder request) { request.header("Content-Type", MediaType.APPLICATION_JSON_VALUE); } java.lang.Exception: Method requestFilter should take only a single HttpRequest parameter
is there any other method to pass headers in provider tests? Using dependencies : au.com.dius:pact-jvm-provider-spring:4.0.10" au.com.dius:pact-jvm-provider-junit:4.010