[英]What is the default name of embedded H2 database in Spring Boot?
[英]Spring boot + Embedded Kafka + h2 database + unit Tests
所以我正在使用這個例子Embedded Kafka和this
我稍微改變了這個例子,並用一些數據庫(如 h2 db)更新了 kafka 監聽器。
現在在我的單元測試中,當我想檢查數據庫中的數據是否可用時,我得到了 NULL。 另外我不確定如何手動檢查數據庫,因為 h2 是基於內存的數據庫。
這是更新的部分:在接收器類中
@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();
}
在單元測試中:
@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));
}
但是 dt 總是為空。 我也無法檢查數據庫,因為它在測試停止后停止。 有人知道如何使它可行嗎?
您是否在測試屬性文件中設置了屬性“kafkatest.topic”? 這可能是您的聽眾沒有收聽指定主題的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.