I have two different contract mS scenarios:
1st- Direct mS-mS Contract testing where i'm using RequestResponsePact to create Pact.
2nd- There is a mq between two microServices called Kafka where i'm using MessagePact to create Pact.
I am not being allowed to use the same buildername for both Contracts.
Code:
//Contract 1
@Rule
public PactProviderRuleMk2 rule= new PactProviderRuleMk2("provider1", "localhost", 8080,PactSpecVersion.V3, this);
@Pact(provider="provider1", consumer="consumer")
public RequestResponsePact createPact(PactDslWithProvider builder) {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return builder.given("/Any Given Statement/)
.uponReceiving("/Any Receiving Statement/")
.path("/path/")
.method("GET")
.willRespondWith()
.headers(headers)
.status(200)
.body(/body/)
.toPact();
}
@Test
@PactVerification({"provider1", "createPact"})
public void runTest(){
//Asser test for StatusCode
}
//Contract 2
@Rule
public MessagePactProviderRule messageProviderProcessOrder = new MessagePactProviderRule("provider2",PactSpecVersion.V3);
@Pact(provider = "provider2", consumer = "consumer") //SameConsumer
public MessagePact msgresponse(MessagePactBuilder builder) throws ClassNotFoundException, JsonProcessingException, SQLException, InterruptedException {
Map<String, String> metadata = new HashMap<String, String>();
metadata.put("contentType", "application/json");
return builder.given(/Any Given Statement/)
.expectsToReceive("/Any Receiving Statement/")
.withMetadata(metadata)
.withConten(//body/)
.toPact();
}
@Test
@PactVerification({"provider2", "msgresponse"})
public void test() throws Exception {
//Asser test for StatusCode
}
Note: When I ran these two contracts individually, it gives Success and Pact files were created. When I ran them both, Below error message was thrown:
Error:
Tests run: 2, Failures: 2, Errors: 0, Skipped: 0, Time elapsed: 11.328 sec <<< FAILURE! - in com.att.udas.xxx.yyy.pact.PactTestSuit
test on test(com.att.udas.xxx.yyy.pact.ConsumerMessagePact)(com.att.udas.xxx.yyy.pact.ConsumerMessagePact) Time elapsed: 1.228 sec <<< FAILURE!
java.lang.UnsupportedOperationException: Method msgresponse does not conform required method signature 'public RequestResponsePact xxx(PactDslWithProvider builder)'
runTest on runTest(com.att.udas.manage.account.pact.ConsumerMessagePact)(com.att.udas.manage.account.pact.ConsumerMessagePact) Time elapsed: 0.005 sec <<< FAILURE!
java.lang.UnsupportedOperationException: Method msgresponse does not conform required method signature 'public RequestResponsePact xxx(PactDslWithProvider builder)'