java - 如何使用 Junit 5 通过 Testcontainer 运行 Kafka 集成测试

标签 java apache-kafka integration-testing junit5 testcontainers

我正在尝试为我的 Kafka 消费者编写集成测试。
我正在使用 JUnit 5,因此我无法使用 @Rule 初始化它,我在 @Container 初始化中看到的示例是不太好用。

我尝试将 Junit 版本更改为 Junit 4,但它会损害我的其他测试(因此我需要继续使用 Junit 5) .

我尝试在 Junit 4 中使用此示例: 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 导入:

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. 我错过了什么?

最佳答案

以下是我感兴趣的设置:

...
<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());
    }
}

关于java - 如何使用 Junit 5 通过 Testcontainer 运行 Kafka 集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57509190/

相关文章:

java - Android Studio 1.5.1 重复文件复制到 APK META-INF/NOTICE.txt commons-codec-1.9.jar ...?

java - 为什么消费者在使用 Java 客户端 API 在 DC/OS 上使用来自 Kafka 的消息时挂起?

perl - 如何在 Alpine 图像上安装 perl Kafka::Connection

ruby - 使用 Minitest 对 protected 或私有(private)方法进行 stub

java - Launch4j重定向下载java版本

java - 可编辑的组合框有问题

apache-kafka - 在 Windows 机器上设置并运行 KSQL

gradle - Gradle:构建失败后清理资源

java - 来自 pom.xml 的 Maven 解析器、Shrinkwrap

java - 使用 Netbeans 和 Java 的 RESTful Web 服务问题