Radek Stolarczyk
11/21/2023, 11:35 AM@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "ProviderCDCT",
providerType = ProviderType.ASYNCH,
pactVersion = PactSpecVersion.V3)
public class ConsumerContractTest {
@Pact(consumer = "ConsumerCDCT")
public MessagePact createMyPact(MessagePactBuilder builder) {
Map<String, String> metadata = new HashMap<>();
metadata.put("content-type", "application/json");
DslPart sqsBody = new PactDslJsonBody()
.stringType("messageId", "1")
.stringValue("receiptHandle", "testRadek")
.stringType("eventSource", "aws:sqs")
.stringType("awsRegion", "us-east-1")
.object("messageAttributes")
.closeObject();
return builder
.expectsToReceive("a message with information")
.withMetadata(metadata)
.withContent(sqsBody)
.toPact();
}
//This method is needed to create the pact files.
@Test
@PactTestFor(pactMethod = "createMyPact")
public void testMyMethod(List<Message> messages) {
}
}
My Provider Class:
@PactBroker(scheme = "https", host = "",
authentication = @PactBrokerAuth(token = ""))
@Provider("ProviderCDCT")
@Consumer("ConsumerCDCT")
public class ProviderContractTest {
private final ObjectMapper objectMapper = new ObjectMapper();
@BeforeEach
public void setUp(PactVerificationContext context) {
context.setTarget(new MessageTestTarget(List.of("Provider")));
System.setProperty("pact.verifier.publishResults",
System.getenv("PACT_BROKER_PUBLISH_VERIFICATION_RESULTS") == null ? "true" : "true");
}
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void testTemplate(PactVerificationContext context) {
context.verifyInteraction();
}
@PactVerifyProvider("a message with information")
public MessageAndMetadata providerMessage() throws Exception {
Map<String, String> metadata = new HashMap<>();
metadata.put("content-type", "application/json");
Map<String, Object> mainMessage = new HashMap<>();
mainMessage.put("messageId", "");
mainMessage.put("receiptHandle", "testRadek");
mainMessage.put("eventSource", "aws:sqs");
mainMessage.put("awsRegion", "us-east-1");
mainMessage.put("messageAttributes", new HashMap<>());
return new MessageAndMetadata(objectMapper.writeValueAsBytes(mainMessage), metadata);
}
}
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)