Skip to content

Instantly share code, notes, and snippets.

@krishnanunnijs
Created April 30, 2024 17:47
Show Gist options
  • Save krishnanunnijs/54dbe6cc0e95b4123b5c5697af50bebd to your computer and use it in GitHub Desktop.
Save krishnanunnijs/54dbe6cc0e95b4123b5c5697af50bebd to your computer and use it in GitHub Desktop.
Contract Test using PACT
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.consumer.junit5.PactConsumerTestExt;
import au.com.dius.pact.consumer.junit5.PactTestFor;
import au.com.dius.pact.core.model.RequestResponsePact;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import java.util.HashMap;
import java.util.Map;
@ExtendWith(PactConsumerTestExt.class)
public class ApiContractTest {
@PactTestFor(providerName = "ProviderService", hostInterface = "localhost", port = "8080")
public class PactConsumerTest {
@Pact(consumer = "ConsumerService")
public RequestResponsePact createPact(PactDslWithProvider builder) {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
return builder
.given("Test State")
.uponReceiving("A request to /api/data")
.path("/api/data")
.method("GET")
.willRespondWith()
.status(200)
.headers(headers)
.body("{\"name\":\"test\",\"status\":\"success\"}")
.toPact();
}
@Test
@PactTestFor(pactMethod = "createPact")
void testConsumerService() {
// Here you would use an HTTP client to make the request to the provider API
// and verify the response matches the expected results defined in the pact
}
}
}
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.consumer.junit5.PactConsumerTestExt;
import au.com.dius.pact.consumer.junit5.PactTestFor;
import au.com.dius.pact.core.model.RequestResponsePact;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import java.util.HashMap;
import java.util.Map;
@ExtendWith(PactConsumerTestExt.class)
public class GenericApiContractTest {
// Utility class for creating PACT contracts
static class PactContractGenerator {
static RequestResponsePact createContract(String providerName, String interactionDescription,
String requestPath, String requestMethod,
int expectedStatus, String responseBody) {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
return PactDslWithProvider
.consumer("ConsumerService")
.given("Test State")
.uponReceiving(interactionDescription)
.path(requestPath)
.method(requestMethod)
.willRespondWith()
.status(expectedStatus)
.headers(headers)
.body(responseBody)
.toPact();
}
}
@PactTestFor(providerName = "ProviderService", hostInterface = "localhost", port = "8080")
public class PactConsumerTest {
@Pact(consumer = "ConsumerService")
public RequestResponsePact createPact() {
// Example usage of the utility class
return PactContractGenerator.createContract("ProviderService",
"A request to /api/data", "/api/data", "GET",
200, "{\"name\":\"test\",\"status\":\"success\"}");
}
@Test
@PactTestFor(pactMethod = "createPact")
void testConsumerService() {
// Implement your HTTP request and response verification here
}
}
}
@krishnanunnijs
Copy link
Author

import au.com.dius.pact.provider.junit5.PactVerificationContext;
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider;
import au.com.dius.pact.provider.junit5.PactVerifyProvider;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(PactVerificationInvocationContextProvider.class)
public class UserProviderTest {

    @TestTemplate
    @ExtendWith(PactVerificationInvocationContextProvider.class)
    void pactVerificationTestTemplate(PactVerificationContext context) {
        context.verifyInteraction();
    }

    @PactVerifyProvider("A request for user")
    public String verifyGetUser() {
        return "{\"id\": 1, \"name\": \"John Doe\"}";
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment