So I am using this example Embedded Kafka and this too
I have changed this example little bit and updated the kafka listener with some database (Like h2 db).
Now in my unit test when I want to check that data is available in DB or not I am getting NULL. Also I am not sure how to check DB manually as h2 is a memory base DB.
here is the updated part: in receiver class
@Autowired
DataTableRepository repository;
@KafkaListener(topics = "${kafkatest.topic}")
public void receive(ConsumerRecord<String, DataTable> consumerRecord) {
LOGGER.info("received payload='{}'", consumerRecord.toString());
repository.save(consumerRecord.value());
latch.countDown();
}
And in unit test :
@Autowired
DataTableRepository repository;
@Test
public void testReceive() throws Exception {
DataTable table = new DataTable(1, "Sending with default template");
template.send(topic, table);
receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
DataTable dt = repository.getOne(table.getId());
assertNotNull(dt);
assertThat(receiver.getLatch().getCount(), equalTo(0L));
}
But dt is always getting null. Also i am not able to check Database also, as it get stopped after test stopped. Anybody has any idea how to make this workable?