- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为我的 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。
最佳答案
以下是我感兴趣的设置:
...
<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/
我有服务器和客户端应用程序。服务器在 docker 容器内启动。我想测试服务器崩溃并恢复的场景。客户端在崩溃前连接到服务器,并在服务器恢复时自动连接到服务器。问题是测试容器随机映射端口,服务器回来后监
我正在使用此依赖项: org.testcontainers postgresql 1.5.1 test 这是容器,通过我的测试启动: @ClassRule publ
我正在部署两个 Testcontainer,一个带有 PostgreSQL (12.0) 镜像,另一个带有 Keycloak (8.0.0)。 PostgreSQL启动成功,但是当Keycloak尝试
问题陈述 我编写了一个程序,它利用 DynamoDb Streams 在发生更新时收到通知,此代码在使用 AWS 服务时工作正常,但似乎无法利用 Testcontainers/Localstack 进
您好,我正在尝试从我的单元测试类中启动一个测试容器。 在我的测试课上,我正在使用以下内容 @Container private static GenericContainer testC
如何在一个 mysql db 容器上创建多个数据库? 或者我是否必须为每个数据库创建 db 容器? 当我尝试从 sql 文件脚本创建它时: @ClassRule public static M
这里有一个在同一网络中创建Testcontainer的好例子:Can Testcontainers create docker network for me if it does not exist?
我使用 javax.mail 实现了一个 POP3 服务器和客户端,只是为了尝试与 Docker 进行集成测试。所以我基于 openjdk:8-jre 镜像创建了两个 Docker 镜像,并将我的 j
假设我的集成测试 (JUnit 5) 中有以下容器: @Container private static GenericContainer databaseContainer = ne
我有这个用于 IT 测试的抽象类: @RunWith(SpringRunner.class) @Import(DbUnitConfig.class) @SpringBootTest(classes =
我在 IDE 中遇到以下错误: Raw use of parameterized class 'GenericContainer' Inspection info: Reports any uses
我正在尝试针对 dockered 数据库运行 dropwizard 的集成测试。 Dropwizard Testcontainers 我试过的: @ClassRule public static fi
我在同一应用程序中同时使用 r2dbc 和 Liquibase。但是,Liquibase 无法使用 r2dbc 运行迁移,因此我需要为此使用单独的 jdbc 驱动程序。 我遵循了解决方案here ,并
我有一个使用MySQL Test容器的小型集成测试。 当我在本地构建时,运行简单的gradlew build命令,它将运行,测试并构建项目。但是,当我尝试使用正式的gradle docker镜像时,该
我正在尝试使用 Testcontainers 对我的 DAO 执行集成测试. 我的目标是在单独的容器中运行每个测试。为此,我添加了 @DirtiesContext注释 - 我的逻辑是,如果我注入(in
我正在使用 PostgreSQL TestContainer 来测试 Spring Boot 中的 Liquibase 架构迁移。我没有任何存储库。我想知道我是否可以查看/访问 TestContain
我正在使用 Testcontainers 通过 docker 启动一个 CockroachDB 实例。我需要通过将属性 sql.defaults.experimental_temporary_tabl
我尝试使用 testcontainers用于与 Oracle 集成测试的库。这是简单的测试: public class SimpleTest { @Rule public Oracle
我正在尝试使用 TestContainer 进行 Spring Boot 应用程序的集成测试。 此类应用程序的数据库驻留在自定义 PostgreSQL Docker 镜像中。 在集成测试中,Appli
我正在使用 Testcontainers 通过 docker 启动一个 CockroachDB 实例。我需要通过将属性 sql.defaults.experimental_temporary_tabl
我是一名优秀的程序员,十分优秀!