如何使用JUnit5通过testcontainer对kafka运行集成测试

66bbxpm5  于 2021-06-04  发布在  Kafka
关注(0)|答案(3)|浏览(622)

我正试图为我的Kafka消费者编写一个集成测试。
我在用 JUnit 5 ,所以我可以׳无法使用初始化 @Rule ,以及我看到的例子 @Container 初始化它不工作。
我试着把我的junit版本改成 Junit 4 但它会损害我的其他测试(所以我需要继续保持) Junit 5 ).
我试着在JUnit4中使用这个例子:https://www.testcontainers.org/modules/kafka/
还有那些 Junit 5 : https://www.hascode.com/2019/01/using-throwaway-containers-for-integration-testing-with-java-junit-5-and-testcontainers/
但它不认识我的注解( @Testcontainers , @Container ).
gradle进口:

  1. testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.0'
  2. testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.4.0'
  3. implementation group: 'org.apache.kafka', name: 'kafka-clients', version: '1.1.1'
  4. testIntegrationImplementation "org.testcontainers:kafka:1.11.4"

我正在上载此代码作为注解:

  1. @Testcontainers
  2. public class KafkaTestContainer implements BeforeAllCallback, AfterAllCallback {
  3. @Container
  4. public KafkaContainer kafkaContainer = new KafkaContainer();
  5. private static final Logger logger = LoggerFactory.getLogger(KafkaTestContainer.class);
  6. @Inject
  7. private KafkaTestContainer() {
  8. try {
  9. } catch (Exception e) {
  10. logger.error(e.getMessage());
  11. }
  12. }
  13. private String getKafkaBootstrapServers(Request request) throws IOException {
  14. return this.kafkaContainer.getBootstrapServers();
  15. }
  16. public void stopKafkaTestContainer() {
  17. // Stop the container.
  18. kafkaContainer.stop();
  19. }
  20. @Override
  21. public void afterAll(ExtensionContext context) throws Exception {
  22. }
  23. @Override
  24. public void beforeAll(ExtensionContext context) throws Exception {
  25. boolean isKafkaRunning = this.kafkaContainer.isRunning();
  26. if(isKafkaRunning) {
  27. logger.info("start Kafka docker!!");
  28. }
  29. }

iskafkarunning值始终为false。
感谢您对kafka测试容器初始化的任何帮助?
我错过了什么??

5t7ly7z5

5t7ly7z51#

缺少以下依赖项:

  1. <dependency>
  2. <groupId>org.testcontainers</groupId>
  3. <artifactId>junit-jupiter</artifactId>
  4. <version>1.13.0</version>
  5. <scope>test</scope>
  6. </dependency>
jckbn6z7

jckbn6z72#

当然有更复杂的方法可以做到这一点,但我认为你只需要:

  1. @Override
  2. public void beforeAll(ExtensionContext context) throws Exception {
  3. while(!this.kafkaContainer.isRunning());
  4. logger.info("start Kafka docker!!");
  5. }

如果可以的话,您应该在混合中添加一个真正的异步框架,并实现更成熟的重试和超时。

bfhwhh0e

bfhwhh0e3#

以下是我的设置:

  1. ...
  2. <properties>
  3. <java.version>1.8</java.version>
  4. <testcontainers.version>1.14.3</testcontainers.version>
  5. <junit.jupiter.version>5.6.2</junit.jupiter.version>
  6. <lettuce.version>5.3.3.RELEASE</lettuce.version>
  7. <lombok.version>1.18.12</lombok.version>
  8. </properties>
  9. <dependencies>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-data-redis</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.security</groupId>
  16. <artifactId>spring-security-messaging</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>io.lettuce</groupId>
  20. <artifactId>lettuce-core</artifactId>
  21. <version>${lettuce.version}</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.kafka</groupId>
  29. <artifactId>spring-kafka</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-devtools</artifactId>
  34. <scope>runtime</scope>
  35. <optional>true</optional>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.projectlombok</groupId>
  39. <artifactId>lombok</artifactId>
  40. <version>${lombok.version}</version>
  41. <optional>true</optional>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-starter-test</artifactId>
  46. <scope>test</scope>
  47. <exclusions>
  48. <exclusion>
  49. <groupId>org.junit.vintage</groupId>
  50. <artifactId>junit-vintage-engine</artifactId>
  51. </exclusion>
  52. </exclusions>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework.kafka</groupId>
  56. <artifactId>spring-kafka-test</artifactId>
  57. <scope>test</scope>
  58. </dependency>
  59. <!-- JUnit 5 dependencies -->
  60. <dependency>
  61. <groupId>org.junit.jupiter</groupId>
  62. <artifactId>junit-jupiter-api</artifactId>
  63. <version>${junit.jupiter.version}</version>
  64. <scope>test</scope>
  65. </dependency>
  66. <dependency>
  67. <groupId>org.junit.jupiter</groupId>
  68. <artifactId>junit-jupiter-params</artifactId>
  69. <version>${junit.jupiter.version}</version>
  70. <scope>test</scope>
  71. </dependency>
  72. <dependency>
  73. <groupId>org.junit.jupiter</groupId>
  74. <artifactId>junit-jupiter-engine</artifactId>
  75. <version>${junit.jupiter.version}</version>
  76. <scope>test</scope>
  77. </dependency>
  78. <!-- Testcontainers dependencies -->
  79. <dependency>
  80. <groupId>org.testcontainers</groupId>
  81. <artifactId>testcontainers</artifactId>
  82. <version>${testcontainers.version}</version>
  83. <scope>test</scope>
  84. </dependency>
  85. <dependency>
  86. <groupId>org.testcontainers</groupId>
  87. <artifactId>junit-jupiter</artifactId>
  88. <version>${testcontainers.version}</version>
  89. <scope>test</scope>
  90. </dependency>
  91. <dependency>
  92. <groupId>org.testcontainers</groupId>
  93. <artifactId>kafka</artifactId>
  94. <version>${testcontainers.version}</version>
  95. <scope>test</scope>
  96. </dependency>
  97. </dependencies>

测试等级exmaple:

  1. @SpringBootTest
  2. @Testcontainers
  3. class KafkaProducerTest {
  4. @Container
  5. public KafkaContainer container = new KafkaContainer();
  6. @Test
  7. void sendMessage() {
  8. assertTrue(container.isRunning());
  9. }
  10. }
展开查看全部

相关问题