I wrote a Cucumber testcase with Citrus Framework where I wanted to test an HTTP Call which internally publishes a message to Kafka. I wrote a testcase to validate this behaviour using "Given, When, Then" syntax. The test case calls the HTTP endpoint as expected and validates the reception of Kafka message on first try but when I execute the same testcase multiple times, it always fails and passes only on first try. This is how my testcase is written:
@When("^I make a http call$")
public void when_scenario() {
runner.when(http()
.client("myHTTPClient")
.send()
.post("/api/endpoint")
.message()
.contentType(MediaType.APPLICATION_XML_VALUE)
.header("X-B3-TraceId", "randomstring")
.body("some XML payload"));
}
@Then("^a kafka message should be sent$")
public void then_scenario() {
runner.then(receive()
.endpoint("myKafkaEndpoint")
.message()
.timeout(5000)
.header("ce_source", "mysourcev1"));
}
This is how the client endpoints are defined
@Bean
public HttpClient myHTTPClient() {
return CitrusEndpoints
.http()
.client()
.requestUrl("http://localhost:10000")
.build();
}
@Bean
public KafkaEndpoint myKafkaEndpoint() {
return CitrusEndpoints
.kafka()
.asynchronous()
.server("localhost:9092")
.topic("my-topic")
.offsetReset("earliest")
.build();
}
This is the error I received even though I can see that the kafka message is being received by the target topic:
org.citrusframework.exceptions.TestCaseFailedException: Action timeout after 5000 milliseconds. Failed to receive message on endpoint: 'my-topic'
What am I missing here?