I've the following configuration
server:
port: 8001
spring:
kafka:
bootstrap-servers: localhost:9092
consumer:
auto-offset-reset: latest
group-id: group-id
value-deserializer: org.springframework.kafka.support.serializer.ErrorHandlingDeserializer
key-deserializer: org.springframework.kafka.support.serializer.ErrorHandlingDeserializer
properties:
spring.deserializer.key.delegate.class: org.apache.kafka.common.serialization.StringDeserializer
spring.deserializer.value.delegate.class: <package..>.config.kafka.KafkaEventDeserializer
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: <package..>.config.kafka.KafkaEventSerializer
kafka:
topic-name: topic-name
group-id: group-id
I want to test the following Kafka Consumer
@KafkaListener(topics = "#{'${kafka.topic}'}",
groupId = "#{'${kafka.group-id}'}")
public void consumeEvents(Event event) throws RuntimeException {
throw new RuntimeException("EventType does not match !");
}
this is my test case
@Test
@SneakyThrows
void consumeEvents_RuntimeExceptionShouldBeThrown() {
...
await().atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> assertThrows(RuntimeException.class, () -> kafkaTemplate.send(receiptTopic, "key", event)));
}
The exception is thrown by the test failed !
It seems that the excpetion is already catched, thus assertThrows
doesn't work properly
How can we handle such case ? Thanks