猿问

如何使用 Junit 5 使用 Testcontainer 对 Kafka 运行集成测试

我正在尝试为我的 Kafka 消费者编写集成测试。
我正在使用JUnit 5,所以我无法使用 对其进行初始化@Rule,而且我看到的初始化示例@Container也无法正常工作。

我试图将我的 Junit 版本更改Junit 4,但它损坏了我的其他测试(所以我需要继续使用Junit 5)。

但它不识别我的注释 ( @Testcontainers@Container)。

摇篮进口:

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.0'

testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.4.0'

implementation group: 'org.apache.kafka', name: 'kafka-clients', version: '1.1.1'

testIntegrationImplementation "org.testcontainers:kafka:1.11.4"

我正在上传此代码作为注释:

@Testcontainers

public class KafkaTestContainer implements BeforeAllCallback, AfterAllCallback {


    @Container

    public KafkaContainer kafkaContainer = new KafkaContainer();

    private static final Logger logger = LoggerFactory.getLogger(KafkaTestContainer.class);


    @Inject

    private KafkaTestContainer() {

        try {


        } catch (Exception e) {

            logger.error(e.getMessage());

        }

    }


    private String getKafkaBootstrapServers(Request request) throws IOException {

        return this.kafkaContainer.getBootstrapServers();

    }



    public void stopKafkaTestContainer() {

        // Stop the container.

        kafkaContainer.stop();


    }


    @Override

    public void afterAll(ExtensionContext context) throws Exception {

    }


    @Override

    public void beforeAll(ExtensionContext context) throws Exception {

        boolean isKafkaRunning = this.kafkaContainer.isRunning();

        if(isKafkaRunning) {

            logger.info("start Kafka docker!!");

        }

    }

isKafkaRunning 值始终为 false。

  1. 对 Kafka 测试容器初始化有任何帮助吗?

  2. 我错过了什么??


潇潇雨雨
浏览 120回答 3
3回答

蛊毒传说

以下是对我有用的设置:...<properties>&nbsp; &nbsp; &nbsp; &nbsp; <java.version>1.8</java.version>&nbsp; &nbsp; &nbsp; &nbsp; <testcontainers.version>1.14.3</testcontainers.version>&nbsp; &nbsp; &nbsp; &nbsp; <junit.jupiter.version>5.6.2</junit.jupiter.version>&nbsp; &nbsp; &nbsp; &nbsp; <lettuce.version>5.3.3.RELEASE</lettuce.version>&nbsp; &nbsp; &nbsp; &nbsp; <lombok.version>1.18.12</lombok.version>&nbsp; &nbsp; </properties>&nbsp; &nbsp; <dependencies>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-data-redis</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.security</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-security-messaging</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>io.lettuce</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>lettuce-core</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${lettuce.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-web</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.kafka</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-kafka</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-devtools</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>runtime</scope>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <optional>true</optional>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.projectlombok</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>lombok</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${lombok.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <optional>true</optional>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-test</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <exclusions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <exclusion>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.junit.vintage</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>junit-vintage-engine</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </exclusion>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </exclusions>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.kafka</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-kafka-test</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <!-- JUnit 5 dependencies -->&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.junit.jupiter</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>junit-jupiter-api</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${junit.jupiter.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.junit.jupiter</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>junit-jupiter-params</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${junit.jupiter.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.junit.jupiter</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>junit-jupiter-engine</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${junit.jupiter.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <!-- Testcontainers dependencies -->&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.testcontainers</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>testcontainers</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${testcontainers.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.testcontainers</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>junit-jupiter</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${testcontainers.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.testcontainers</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>kafka</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${testcontainers.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; </dependencies>测试类示例:@SpringBootTest@Testcontainersclass KafkaProducerTest {&nbsp; &nbsp; @Container&nbsp; &nbsp; public KafkaContainer container = new KafkaContainer();&nbsp; &nbsp; @Test&nbsp; &nbsp; void sendMessage() {&nbsp; &nbsp; &nbsp; &nbsp; assertTrue(container.isRunning());&nbsp; &nbsp; }}

青春有我

您缺少以下依赖项:<dependency>&nbsp; &nbsp; <groupId>org.testcontainers</groupId>&nbsp; &nbsp; <artifactId>junit-jupiter</artifactId>&nbsp; &nbsp; <version>1.13.0</version>&nbsp; &nbsp; <scope>test</scope></dependency>

慕哥6287543

当然有更复杂的方法可以做到这一点,但我认为你只需要:&nbsp; &nbsp; @Override&nbsp; &nbsp; public void beforeAll(ExtensionContext context) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; while(!this.kafkaContainer.isRunning());&nbsp; &nbsp; &nbsp; &nbsp; logger.info("start Kafka docker!!");&nbsp; &nbsp; }如果可行,您应该添加一个真正的异步框架,并实施更成熟的重试和超时。
随时随地看视频慕课网APP

相关分类

Java
我要回答