0
votes

All the examples I could find on PACT has one to one mapping of producer api to consumer api. In our case all of our consumer apis will call more than one producer apis.

We are using pact Jvm. Our consumer contract tests run against consumer service. Producer mocks are defined in independent functions with @Pact attribute and contract tests have @PactTestFor(pactMethod = attribute, here "pactmockmethodname" is the function name which has producer mock. This setup is working fine. Now, We got into a case where our consumer api has to call more than one producer api. I tried defining multiple pact mocks but was not able to hook them to test as @PactTestFor attribute takes only one pactMethod. What is the suggested approach for this case.

1

1 Answers

0
votes

Answering my above question. Found that we can add multiple pact mocks with builder. Example

Consumer api calls two producer methods /user/1 and /user/account/1 we can define pact mock as below.

  @Pact(consumer = "CONSUMER")
    fun getUser(builder: PactDslWithProvider): RequestResponsePact {
        return builder
            .uponReceiving("get user basic info request")
            .path("/user/1")
            .......  // define response status code and body as required
            .uponReceiving("get user account info request")
            .path("/user/account/1")
            ......  // define response status code and body as required
            .toPact()
     }

Consumer contract test can be something as below

    @Test
    @PactTestFor(pactMethod = "getUser")
    fun `should respond with user info`() {
       // consumer api call
        val result = restTemplate.getForEntity("/customers/1",String::class.java) 
        val expected = ... // have expected here
        assertEquals(HttpStatus.OK, result.statusCode)
        assertEquals(expected, result.body, false)
    }