Rapository在与Kafkalistener的集成测试中无法连接到Postgres(testContainers)

发布于 2025-01-20 10:10:53 字数 1912 浏览 0 评论 0原文

我使用带有Postgres DB的测试容器运行集成测试。当我使用kafkatemplate将JSON消息发送到Kafka主题时,我会得到SQL例外:

[W] SqlExceptionHelper - SQL Error: 0, SQLState: 42P01 
[E] SqlExceptionHelper - ERROR: relation "test_db.test_schema" does not exist

could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

我的测试如下:

@Autowired
TestRepository repository;

@Autowired
KafkaTemplate<String, String> kafkaTemplate;

private void publishFile(String fileName) {
    kafkaTemplate.send("test.topic", "1", readFile(fileName));
}

@Test
public void test_1() {
    publishFile("json/test_1.json");
    await().atMost(5, SECONDS).until(()-> repository.findAll().size() == 1);
}

当我调用PublishFile消息时,到达@kafkalistener,

@KafkaListener(topics = "test.topic")
public void onRecieve(@Payload String json, @Header(name = RECIEVED_MESSAGE_KEY) String key) {
    TestEntity entity = mapper.readValue(json, TestEntity.class);
    repository.save(entity);
}

但是当我直接在类似的测试中使用存储库时:

@Test
public void test_1() {
    repository.findAll();
}

一切正常。我可以从存储库写信并阅读。

另外,我可以在此测试中访问存储库:

@Test
public void test_1() {
    TestEntity entity = mapper.readValue(readFile("json/test_1.json"), TestEntity.class);
    repository.save(entity);
    publishFile("json/test_1.json");
    log.info(repository.findAll());
    await().atMost(5, SECONDS).until(()-> repository.findAll().size() == 1);
}

在主线程中,我可以写入存储库,然后在sendig消息到@kafkalistener之后,我可以从存储库中阅读。但是在@kafkalistener线程中,我不能。我想应该存在相同的测试上下文,但看起来不是。如何使用存储库与@kafkalistener线程中的Postgres容器进行互相关?

I run integrations tests using test containers with Postgres DB. When I send json message to a kafka topic using KafkaTemplate I getting SQL exception:

[W] SqlExceptionHelper - SQL Error: 0, SQLState: 42P01 
[E] SqlExceptionHelper - ERROR: relation "test_db.test_schema" does not exist

could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

My test looks like this:

@Autowired
TestRepository repository;

@Autowired
KafkaTemplate<String, String> kafkaTemplate;

private void publishFile(String fileName) {
    kafkaTemplate.send("test.topic", "1", readFile(fileName));
}

@Test
public void test_1() {
    publishFile("json/test_1.json");
    await().atMost(5, SECONDS).until(()-> repository.findAll().size() == 1);
}

When I call publishFile message arrives to a @KafkaListener

@KafkaListener(topics = "test.topic")
public void onRecieve(@Payload String json, @Header(name = RECIEVED_MESSAGE_KEY) String key) {
    TestEntity entity = mapper.readValue(json, TestEntity.class);
    repository.save(entity);
}

But when I use repository directly in test like this:

@Test
public void test_1() {
    repository.findAll();
}

Everything ok. I can write to and read from repository.

Also I have access to a repository in this test:

@Test
public void test_1() {
    TestEntity entity = mapper.readValue(readFile("json/test_1.json"), TestEntity.class);
    repository.save(entity);
    publishFile("json/test_1.json");
    log.info(repository.findAll());
    await().atMost(5, SECONDS).until(()-> repository.findAll().size() == 1);
}

In main thread I can write to a repository and after sendig message to a @KafkaListener I can read from repository. But In @KafkaListener thread I can not. I suppose there should be the same test context but looks like its not. How can I use repository to interract with Postgres container in @KafkaListener thread?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文