在每次 junit 测试后重置 kafka testcontainer 而无需清除和重新创建 testcontainer

Reset kafka testcontainers without clearing and recreating the testcontainer after every junit test

我在 JUnit5 中使用 kafka 测试容器。有人可以告诉我如何在每次测试后从 Kafka testcontainer 中删除数据,这样我就不必每次都销毁和重新创建 kafka testcontainer。

将容器变量设为静态

Containers declared as static fields will be shared between test methods. They will be started only once before any test method is executed and stopped after the last test method has executed

https://www.testcontainers.org/test_framework_integration/junit_5/

不过,请确保您不在测试之间共享状态。例如,如果您想测试创建主题、生成主题、使用主题和删除主题,那么所有这些都应该在一个测试中。尽管您可以调用单独的非测试方法。

也就是说,理想情况下每个测试都应该使用唯一的主题名称。一个描述测试的,也许。

此外,如文档所述,您不能使用并行测试运行程序