'contractVerifierMessaging.receive is null

I'm setting up contract tests for Kafka messaging with Test Containers in a way described in spring-cloud-contract-samples/producer_kafka_middleware/. Works good with Embedded Kafka but not with TestContainers.

When I try to run the generated ContractVerifierTest:

public void validate_shouldProduceKafkaMessage() throws Exception {
    // when:
    triggerMessageSent();

    // then:
    ContractVerifierMessage response = contractVerifierMessaging.receive("kafka-messages",
    contract(this, "shouldProduceKafkaMessage.yml"));

Cannot invoke "org.springframework.messaging.Message.getPayload()" because "receive" is null is thrown

Kafka container is running, the topic is created. When debugging receive method I see the message is null in the message(destination);

Contract itself:

    label("triggerMessage")
    input {
        triggeredBy("triggerMessageSent()")
    }

    outputMessage {
        sentTo "kafka-messages"
        body(file("kafkaMessage.json"))

Base test configuration:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes =  {TestConfig.class, ServiceApplication.class})
@Testcontainers
@AutoConfigureMessageVerifier
@ActiveProfiles("test")
public abstract class BaseClass {

What am I missing? Maybe a point of communication between the container and ContractVerifierMessage methods?



Solution 1:[1]

Resolved the issue in the following way:

  1. ContractVerifierTest runs OK with

    ./gradlew clean build publishToMavenLocal --exclude-task test

not just with contractTests task OR right mouse click like with ordinary test classes.

  1. To trigger message sending to Kafka, application controller should be used, not getProducer.send() as I did.

To conclude, the setup should be copied from samples sources, no experiments.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ivanna