Testcontainers Desktop 提供对调查和调试的支持

AtomicJar 推出 Testcontainers Desktop 免费应用

AtomicJar 推出了免费的 Testcontainers Desktop 应用,该应用支持 Testcontainers 开源框架,允许在单元测试中使用真实的依赖项。Testcontainers Desktop 提供了 Testcontainers 框架中不具备的多种功能,并增强了调试和调查的支持,例如支持固定端口、冻结和重用容器,以及轻松切换容器运行时。

Testcontainers Cloud 背景

2021 年 11 月,AtomicJar 推出了 Testcontainers Cloud 公测版,允许团队在各种环境和配置中运行容器。该产品需要桌面应用程序支持,因此开发了 Testcontainers Cloud Desktop。现在,Testcontainers Cloud Desktop 工具被捐赠给社区,成为免费的 Testcontainers Desktop 应用。

Testcontainers Desktop 主要功能

  1. 固定端口:允许设置固定端口,替代 Testcontainers 默认的随机端口,便于从 IntelliJ 等工具进行调试。
  2. 冻结容器:防止容器关闭,使测试无限运行,便于调查和调试。分析完成后可以取消冻结,恢复测试执行和默认的清理逻辑。
  3. 切换容器运行时:支持多种解决方案,如 OrbStack、Colima、Rancher Desktop 和 Podman。

Testcontainers 框架简介

Testcontainers 框架允许在 JUnit 测试中使用 Docker 容器中的数据库、消息代理等服务。首先需要在 pom.xml 中配置 Testcontainers BOM:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.testcontainers</groupId>
      <artifactId>testcontainers-bom</artifactId>
      <version>1.19.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

然后可以使用依赖项,例如 MySQL:

<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>mysql</artifactId>
    <scope>test</scope>
</dependency>

或者使用 Gradle 管理依赖:

implementation platform('org.testcontainers:testcontainers-bom:1.19.0') testImplementation('org.testcontainers:mysql')

容器注入与使用

在 JUnit 测试中注入 Docker 容器,例如 MySQL 容器:

@Container
private static final MySQLContainer mySQLContainer = new MySQLContainer<>("mysql:8.1.0");

可以覆盖默认值:

@Container
private static final MySQLContainer mySQLContainer = new MySQLContainer<>("mysql:8.1.0")
    .withDatabaseName("myDatabase")
    .withUsername("myUsername")
    .withPassword("myPassword");

Testcontainers 提供了多种模块,方便集成 Docker 容器。也可以使用 GenericContainer 类:

@Container
private static final GenericContainer myContainer = new GenericContainer("redis:7.2.1")         
    .withExposedPorts(42);

更多资源与支持

  • 文档:提供更多关于 Testcontainers for Java 的信息。
  • 演示视频:AtomicJar 发布了 Testcontainers Desktop 的介绍视频。
  • 安装包:提供 Mac、Windows 和 Linux 系统的安装包。
  • 指南:提供简单本地开发的详细指南。
  • 开发者讨论:AtomicJar 的开发者和创始人通过 YouTube 演示和讨论了 Testcontainers Desktop。
阅读 43
0 条评论