Hi Team, Greetings of the day!! I am setting up...
# pact-jvm
c
Hi Team, Greetings of the day!! I am setting up a Pact contract test for Kafka (MessagePact) and need some guidance. Do we need to spin up a real Kafka instance when writing a consumer contract test? Also, is a Kafka instance required for provider verification? If there are any examples available, please share them. Thanks.
y
c
Thanks for confirming and sharing the link. I will go through it.
@Yousaf Nabi (pactflow.io) Actually, I am using below two repositories. When I ran consumer test, pact was generated and stored in Pactflow. But when I tried to validate it from provider side, I encountered following exception. Can you tell me what is wrong here? https://github.com/pactflow/example-provider-java-kafka https://github.com/pactflow/example-consumer-java-kafka Exception
Copy code
ProductsKafkaProducerTest > testTemplate(Pact, Interaction, PactVerificationContext) > io.pactflow.example.kafka.ProductsKafkaProducerTest.initializationError FAILED
    au.com.dius.pact.core.support.json.JsonException: Invalid JSON (1:4), found unexpected character 'm'
        at app//au.com.dius.pact.core.support.json.JsonLexer.unexpectedCharacter(JsonParser.kt:87)
        at app//au.com.dius.pact.core.support.json.JsonLexer.nextToken(JsonParser.kt:78)
        at app//au.com.dius.pact.core.suppor
Another Question Do we need to mock Kafka message ourselves, or does Pact handle it behind the scenes using a JUnit annotation? Could you please clarify?
@Yousaf Nabi (pactflow.io): Any input from your side above issue ?
y
Hi, Hmm not sure there, Could you try and load the provider file from disk, rather than the pactflow broker, to eliminate that part of your setup. It does work in CI, which platform are you running on, and do you have all the appropriate environment variables set? Can you add verbose logging, are you using gradle, or maven or something else?
c
All the environment variables are correctly set as specified in the repository, such as PACT_BROKER_TOKEN PACT_BROKER_BASE_URL PACT_BROKER_HOST I tried both Gradle and Maven and platform window and Linux but encountered same error. I will try again as per the suggestion.
@Yousaf Nabi (pactflow.io): Do we need to manually mock the Kafka message, or does Pact handle it automatically behind the scenes? Could you clarify?
y
you just need to produce the message that would be sent via kafka. pact requires it as a base64 encoded header, with another header for metadata. this is handled transparently and the user maps their message generator, with the message description from the consumer test. kafka doesn’t need to be started, we are testing the message not the transport with message pact
c
Hi @Yousaf Nabi (pactflow.io), I am working on setting up a sample Consumer-Driven Contract test for a Kafka-based service using Pact. The consumer test successfully generates the pact file and publishes it to Pactflow. I am encountering an issue during provider verification. I have attached consumer test (SampleServiceTest.java) provider test (ProviderPactTest.java) for reference. Initially, when the @PactVerifyProvider annotation was not used, I received the attached error. Refer image (ErrorwhenPactVerifyProviderAnnotationNotAdded.JPG) After adding the @PactVerifyProvider annotation to the provider verification method, the issue still persists — the error consistently states that the type cannot be resolved. Error When @*PactVerifyProvider annotation added* [ERROR] symbol: class PactVerifyProvider [ERROR] location: class consumer.ProviderPactTest I am using the JUnit 5 approach and have included pom.xml files for both the consumer and provider projects. The pact file is being loaded from local pacts directory. Could you please help me resolve this error? Also, I would appreciate it if you could confirm whether the overall approach I am following is correct for testing Kafka services with Pact and JUnit 5. Looking forward to your response. Thanks,
y
Thanks for the repro, I’ll take a look and get back to you, it might be tomorrow.
c
I am able to validate from provider side. Please ignore my above message. Thanks.
y
great stuff, thanks for confirming buddy!
c
HI @Yousaf Nabi (pactflow.io), Greetings of the day. Do we need to have NATS server running while setting up a Pact contract test for NATS communication? Do pact natively support NATS communication. how we are going to achieve if PACT does not support NATS. Can you clarify this. Thanks.
y
pact natively supports http. it supports other protocols, by being protocol agnostic, and only caring about the message contents/structure - this is message pact. there is a pact plugin framework, which would allow you to create a plugin which is aware of nats as a transport protocol and/or custom content type which pact doesn’t natively support. I haven’t used nats before, but for message pact, is shouldn’t be necessary if you can segment your code so that your message producer and consumer are split from the transport mechanism. If you have can provide any more guidance on nats, how you use it, how you currently test it, and what you would like to achieve from pact, we can guide you how best to achieve your desired outcome
c
Thanks for the clarification. To provide bit more context on our setup: Some of our product services use NATS communication for internal service-to-service communication, where they publish and subscribe to various NATS subjects. Our goal with Pact is to define contract tests between these services. Specifically, we are looking to validate two types of NATS communication patterns, if possible • Request-Reply • Fire-and-Forget We would appreciate your guidance on how best to approach this using Pact. Thanks.