为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

潇潇雨雨 2023-06-08 19:17:30
我正在尝试为我的 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"我正在上传此代码作为注释:@Testcontainerspublic 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。对 Kafka 测试容器初始化有任何帮助吗?我错过了什么??
查看完整描述

3 回答

?
蛊毒传说

TA贡献1895条经验 获得超3个赞

以下是对我有用的设置:


...

<properties>

        <java.version>1.8</java.version>

        <testcontainers.version>1.14.3</testcontainers.version>

        <junit.jupiter.version>5.6.2</junit.jupiter.version>

        <lettuce.version>5.3.3.RELEASE</lettuce.version>

        <lombok.version>1.18.12</lombok.version>

    </properties>


    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-data-redis</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.security</groupId>

            <artifactId>spring-security-messaging</artifactId>

        </dependency>

        <dependency>

            <groupId>io.lettuce</groupId>

            <artifactId>lettuce-core</artifactId>

            <version>${lettuce.version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.kafka</groupId>

            <artifactId>spring-kafka</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-devtools</artifactId>

            <scope>runtime</scope>

            <optional>true</optional>

        </dependency>

        <dependency>

            <groupId>org.projectlombok</groupId>

            <artifactId>lombok</artifactId>

            <version>${lombok.version}</version>

            <optional>true</optional>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

            <exclusions>

                <exclusion>

                    <groupId>org.junit.vintage</groupId>

                    <artifactId>junit-vintage-engine</artifactId>

                </exclusion>

            </exclusions>

        </dependency>

        <dependency>

            <groupId>org.springframework.kafka</groupId>

            <artifactId>spring-kafka-test</artifactId>

            <scope>test</scope>

        </dependency>


        <!-- JUnit 5 dependencies -->

        <dependency>

            <groupId>org.junit.jupiter</groupId>

            <artifactId>junit-jupiter-api</artifactId>

            <version>${junit.jupiter.version}</version>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.junit.jupiter</groupId>

            <artifactId>junit-jupiter-params</artifactId>

            <version>${junit.jupiter.version}</version>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.junit.jupiter</groupId>

            <artifactId>junit-jupiter-engine</artifactId>

            <version>${junit.jupiter.version}</version>

            <scope>test</scope>

        </dependency>


        <!-- Testcontainers dependencies -->

        <dependency>

            <groupId>org.testcontainers</groupId>

            <artifactId>testcontainers</artifactId>

            <version>${testcontainers.version}</version>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.testcontainers</groupId>

            <artifactId>junit-jupiter</artifactId>

            <version>${testcontainers.version}</version>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.testcontainers</groupId>

            <artifactId>kafka</artifactId>

            <version>${testcontainers.version}</version>

            <scope>test</scope>

        </dependency>

    </dependencies>

测试类示例:


@SpringBootTest

@Testcontainers

class KafkaProducerTest {


    @Container

    public KafkaContainer container = new KafkaContainer();


    @Test

    void sendMessage() {

        assertTrue(container.isRunning());

    }

}


查看完整回答
反对 回复 2023-06-08
?
青春有我

TA贡献1784条经验 获得超8个赞

您缺少以下依赖项:


<dependency>

    <groupId>org.testcontainers</groupId>

    <artifactId>junit-jupiter</artifactId>

    <version>1.13.0</version>

    <scope>test</scope>

</dependency>


查看完整回答
反对 回复 2023-06-08
?
慕哥6287543

TA贡献1831条经验 获得超10个赞

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


    @Override

    public void beforeAll(ExtensionContext context) throws Exception {

        while(!this.kafkaContainer.isRunning());

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

    }

如果可行,您应该添加一个真正的异步框架,并实施更成熟的重试和超时。


查看完整回答
反对 回复 2023-06-08
  • 3 回答
  • 0 关注
  • 93 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信