Sergii Lisnychyi
09/07/2022, 2:34 PMprovider
test.
@Provider("provider-adapter")
@PactFolder("pacts")
class ProviderContractTest {
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider::class)
fun testTemplate(pact: Pact?, interaction: Interaction?, context: PactVerificationContext?) {
context?.verifyInteraction()
}
@BeforeEach
fun before(context: PactVerificationContext?) {
context?.target = MessageTestTarget()
}
@State("ProviderState")
fun providerState() {
}
@Suppress("unused")
@PactVerifyProvider("some test")
private fun verifyProvider(): String {
return "{ \"name\" : \"test\"}"
}
}
After test run i am getting the following message:
Only message interactions can be used with an AMPQ test target
java.lang.UnsupportedOperationException: Only message interactions can be used with an AMPQ test target
at au.com.dius.pact.provider.junit5.MessageTestTarget.prepareRequest(TestTarget.kt:184)
at au.com.dius.pact.provider.junit5.PactVerificationExtension.beforeTestExecution(PactVerificationExtension.kt:117)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeTestExecutionCallbacks$5(TestMethodTestDescriptor.java:188)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeMethodsOrCallbacksUntilExceptionOccurs$6(TestMethodTestDescriptor.java:199)
Zakir Hossain
09/08/2022, 1:00 PMexpected status of 200 but was 401
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactVerificationTestTemplate(PactVerificationContext context, HttpRequest request){
request.addHeader("Authorization", "Bearer a_valid_jwt_token");
context.verifyInteraction();
}
Thanks in advance.Ashish Merani
09/08/2022, 10:11 PM"au.com.dius.pact.provider:junit5:4.1.39"
I have specified @PactFolder("src/contractTest/")
but the test fails with the following error
1.1) Connect to localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)
It fails on context.verifyInteraction();
under
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void testTemplate(PactVerificationContext context) {
context.verifyInteraction();
}
I am trying to understand why it is making the call to localhost when I have specified a local path under @PactFolder annotation. Can anyone help?Uzma Khan
09/12/2022, 6:31 PMUzma Khan
09/12/2022, 6:51 PMZakir 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.Dwiparna Pal
09/13/2022, 10:47 AMHello, Hello, the import for the below is not happening for me.
Dwiparna Pal
09/13/2022, 10:47 AMimport au.com.dius.pact.provider.junit5.AmpqTestTarget
Dwiparna Pal
09/13/2022, 10:48 AM<dependency>
<groupId>au.com.dius.pact.provider</groupId>
<artifactId>junit5</artifactId>
<version>4.3.14</version>
any help will be appreciated. ThanksÉdouard Lopez
09/14/2022, 8:48 AM@Provider("ms.seller-opt-out")
@Consumer("ms.channel-management-rule-applier")
…
public class SellerOptOutRuleRuleApplierPublisherPactTest {
I see you Ashish Merani
09/14/2022, 11:01 PMid
to ids
the contract test would fail?
{
"id": "5",
"type": "something"
}
I looked at .stringValue which only compares "5" and not id
itself.Matt (pactflow.io / pact-js / pact-go)
Saurabh Goel
09/15/2022, 12:59 PM<https://ci-server/api/repos/comp/proj/builds?branch=ct_update_consumer_ver&pact_url=${pactbroker.pactUrl}&pact_consumer=${pactbroker.consumerName}&pact_consumer_branch=${pactbroker.consumerVersionBranch}&pact_consumer_version=${pactbroker.consumerVersionNumber}>
and pact tests are getting executed with simple gradle run test which runs everything else too
- ./gradlew clean build -PisDroneCI=true -Prevision=${DRONE_COMMIT} -Ptag=${DRONE_TAG} -PbuildNumber=${DRONE_BUILD_NUMBER} -PbuildVersion=$(cat .tags) --stacktrace
Rubén Pérez
09/16/2022, 10:37 AMcontract_requiring_verification_published
event), that triggers the needed provider builds once a contract change is detected. As a parameter, it sends the URL of the pact version to verify.
Also, in our JVM provider applications so far we have set the consumer version selectors in Gradle, so that the versions to verify are the latests from the main branch and the latests deployed/released. In our case using the workaround rawjson
, given that the @ConsumerVersionSelector
annotation didn't support branches or deployments.
So my question is how do I let Pact know in my provider code that when the build has been triggered by the Broker, what I need to check is the pact version that the webhook has sent instead of the one defined in the rawjson
property?pocomon
09/18/2022, 7:10 PM@TestTarget
public final Target target = new HttpTarget("https", "127.0.0.1", 443, "api", true);
2. I have a job in the pipeline which build the application by running the "mvn clean install" command . The provider test also runs in this scenario and as the environment is not ready it will fail the test and build will not succeed. I want to control the running of pact provider test only when invoked individually in a job for pact provider testing. In all the other cases if anyone invoke the command "mvn clean install" pact provider test cases should not run. How to handle this situation.
Thanks in advance for the help and guidanceAnh Vuong
09/21/2022, 1:54 AMAnh Vuong
09/21/2022, 4:12 PMMax Forsman
09/22/2022, 9:18 AM@Pact(provider="test_provider", consumer="test_consumer")
public RequestResponsePact createPact(PactDslWithProvider builder) {
However, this isn’t compatible with the latest version:
<dependency>
<groupId>au.com.dius.pact.consumer</groupId>
<artifactId>junit5</artifactId>
<version>4.3.14</version>
<scope>test</scope>
</dependency>
java.lang.UnsupportedOperationException: Method testPact does not conform required method signature 'public <http://au.com|au.com>.dius.pact.core.model.V4Pact xxx(PactBuilder builder)'
Could someone point me in the direction of an updated example using the new DSL?Anh Vuong
09/22/2022, 6:22 PMVorashil Farzaliyev
09/26/2022, 10:38 AMVorashil Farzaliyev
09/26/2022, 10:38 AMVorashil Farzaliyev
09/26/2022, 10:38 AMThai Le
09/30/2022, 9:29 PMOleksandr Yanov
10/06/2022, 8:42 AMShan
10/07/2022, 3:15 AMDpact.provider.branch=${GIT_BRANCH}
Dpactbroker.providerBranch=${GIT_BRANCH}
Are they the same?Thai Le
10/07/2022, 6:01 PM12:44:25.963 [main] WARN au.com.dius.pact.provider.junitsupport.loader.PactBrokerLoader - Failed to instantiate the value resolver, using the default
java.lang.IllegalAccessException: class au.com.dius.pact.provider.junitsupport.loader.PactBrokerLoader cannot access a member of class au.com.dius.pact.core.support.expressions.SystemPropertyResolver with modifiers "private"
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361)
at java.base/jdk.internal.reflect.Reflection.ensureMemberAccess(Reflection.java:99)
at java.base/java.lang.Class.newInstance(Class.java:579)
at au.com.dius.pact.provider.junitsupport.loader.PactBrokerLoader.setupValueResolver(PactBrokerLoader.kt:168)
at au.com.dius.pact.provider.junitsupport.loader.PactBrokerLoader.load(PactBrokerLoader.kt:99)
at au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider$resolvePactSources$$inlined$flatMap$lambda$1.invoke(PactJUnit5VerificationProvider.kt:80)
at au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider$resolvePactSources$$inlined$flatMap$lambda$1.invoke(PactJUnit5VerificationProvider.kt:37)
at au.com.dius.pact.core.support.KotlinLanguageSupportKt.handleWith(KotlinLanguageSupport.kt:38)
at au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider.resolvePactSources(PactJUnit5VerificationProvider.kt:80)
at au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider.provideTestTemplateInvocationContexts(PactJUnit5VerificationProvider.kt:41)
at org.junit.jupiter.engine.descriptor.TestTemplateTestDescriptor.lambda$execute$0(TestTemplateTestDescriptor.java:102)
i am not sure what cause this but if i replace @WebMvcTest with @SpringBootTest then i dont get this error, however it requires other external components running (mysql, message broker,...) since the whole spring context is loadedMHNOT
10/10/2022, 6:24 AMpublic class DownloadEndpointsCDCTest extends ConsumerPactTest
{
@Override
protected RequestResponsePact createPact(final PactDslWithProvider aBuilder)
{
final DslPart expectedBodyResponse1 = PactDslRootValue.stringMatcher("^.+$", "whatever");
final DslPart expectedBodyResponse2 = PactDslRootValue.stringMatcher("^.+$",
"<?xml version=\"1.0\" encoding=\"utf-8\"?><example>foo</example>");
return aBuilder//
.given("A XML generation job finished successfully")
.uponReceiving("A request to download XML")
.pathFromProviderState("/xmlresult/${jobId}", "/xmlresult/" + "dummyJobId")
.method("GET")
.willRespondWith()
.status(200)
.headers(Map.of("Content-Type", "application/xml"))
// .body(expectedBodyResponse1)
.body(expectedBodyResponse2)
.toPact();
}
}
Generated contract:
{
"consumer": {
"name": "XXX_Client"
},
"interactions": [
{
"description": "A request to download a XML",
"providerStates": [
{
"name": "A XML generation finished successfully"
}
],
"request": {
"generators": {
"path": {
"dataType": "STRING",
"expression": "/nativeresult/${jobId}",
"type": "ProviderState"
}
},
"headers": {
"RM-MDC-TOKEN": "dummyTraceId"
},
"method": "GET",
"path": "/xmlresult/dummyJobId"
},
"response": {
"body": "<?xml version=\"1.0\" encoding=\"utf-8\"?><example>foo</example>",
"headers": {
"Content-Type": "application/xml"
},
"matchingRules": {
"body": {
"$": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "^.+$"
}
]
}
},
"header": {}
},
"status": 200
}
}
],
"metadata": {
"pact-jvm": {
"version": "4.3.14"
},
"pactSpecification": {
"version": "3.0.0"
}
},
"provider": {
"name": "XXX_Service"
}
}
The verification fails on provider side.
In case we use "*expectedBodyResponse1*" from the above example following exception is thrown (which is comprehensible because the content is not XML):
Content is not allowed in prolog.
at au.com.dius.pact.provider.junit5.PactVerificationContext.verifyInteraction(PactVerificationContext.kt:66)
In case we use "*expectedBodyResponse2*" from the above example following exception is thrown:
_Verifying a pact between XXX_Client and XXX_Service - A request to download a XML has a matching body_
body: $.example.#text Expected '' to match '^.+$'
at au.com.dius.pact.provider.junit5.PactVerificationContext.verifyInteraction(PactVerificationContext.kt:66)
How can we verify that a response of content type "application/xml" is not empty?
Used versions:
Consumer: au.com.dius.pact.consumerjunit4.3.14
Provider: au.com.dius.pact.providerjunit54.3.13Thai Le
10/12/2022, 4:00 PMThai Le
10/12/2022, 9:36 PMmvn -DpipelineBuild=true --activate-profiles maven-profile,provider-pact-test -Dpackbroker.scheme=http -Dpactbroker.host=localhost -Dpactbroker.port=9292 -Dpactbroker.auth.username=pact_workshop -Dpactbroker.auth.password=pact_workshop -Dpact.provider.version="7.0.36-$(git rev-parse --short HEAD)" -Dpactbroker.enablePending=false -Dpact.verifier.publishResults=true --projects 60000-demo-service/demo-service --also-make -f all-modules-pom.xml clean verify
and i see pact tries to verify this version of provider against all versions of consumer. Is there a way to tell pact to verify a particular version of the provider against one version of the consumer? I saw the Consumer Version Selector page (https://docs.pact.io/pact_broker/advanced_topics/consumer_version_selectors) but it does not take the version (tag, branch, environment are supported) and I don't see it accept consumerVersionSelectors params (i checked with mvn help:describe -DgroupId=au.com.dius.pact.provider -DartifactId=maven -Dversion=4.1.17 -Ddetail=true)Anh Vuong
10/13/2022, 5:56 PM