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

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

我正试图为我的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进口:

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。
感谢您对kafka测试容器初始化的任何帮助?
我错过了什么??

5t7ly7z5

5t7ly7z51#

缺少以下依赖项:

<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>1.13.0</version>
    <scope>test</scope>
</dependency>
jckbn6z7

jckbn6z72#

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

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

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

bfhwhh0e

bfhwhh0e3#

以下是我的设置:

...
<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>

测试等级exmaple:

@SpringBootTest
@Testcontainers
class KafkaProducerTest {

    @Container
    public KafkaContainer container = new KafkaContainer();

    @Test
    void sendMessage() {
        assertTrue(container.isRunning());
    }
}

相关问题