Created
April 30, 2024 17:47
-
-
Save krishnanunnijs/54dbe6cc0e95b4123b5c5697af50bebd to your computer and use it in GitHub Desktop.
Contract Test using PACT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
Author
krishnanunnijs
commented
Aug 28, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment