SpringBoot整合Testcontainers实现数据库与中间件集成测试
更新时间:2026年07月30日 08:38:03 作者:张老师技术栈
想知道单元测试和集成测试的核心区别吗,本文带你深入探索Testcontainers如何一键启动Docker容器,轻松搞定MySQL和Redis集成测试,告别Mock依赖,实现真实环境自动销毁,Java/Python开发者必看,让你的测试更可靠,需要的朋友可以参考下
单元测试可以 Mock 掉数据库,但集成测试需要真实环境。Testcontainers 在测试时启动 Docker 容器,用完后自动销毁。
一、引入依赖
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.19.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<version>1.19.3</version>
<scope>test</scope>
</dependency>二、MySQL 集成测试
@SpringBootTest
@Testcontainers
class UserRepositoryTest {
@Container
static MySQLContainer<?> mysql = new MySQLContainer<>("mysql:8.0")
.withDatabaseName("testdb")
.withUsername("test")
.withPassword("test");
@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", mysql::getJdbcUrl);
registry.add("spring.datasource.username", mysql::getUsername);
registry.add("spring.datasource.password", mysql::getPassword);
}
@Autowired
private UserRepository userRepository;
@Test
void testSaveAndFind() {
User user = new User();
user.setName("张三");
user.setEmail("zhangsan@test.com");
userRepository.save(user);
assertThat(user.getId()).isNotNull();
User found = userRepository.findById(user.getId()).orElse(null);
assertThat(found).isNotNull();
assertThat(found.getName()).isEqualTo("张三");
}
}
三、Redis 集成测试
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>redis</artifactId>
<version>1.19.3</version>
<scope>test</scope>
</dependency>@SpringBootTest
@Testcontainers
class RedisCacheTest {
@Container
static GenericContainer<?> redis = new GenericContainer<>(
DockerImageName.parse("redis:7-alpine"))
.withExposedPorts(6379);
@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
registry.add("spring.redis.host", redis::getHost);
registry.add("spring.redis.port", () -> redis.getMappedPort(6379));
}
@Autowired
private StringRedisTemplate redisTemplate;
@Test
void testSetAndGet() {
redisTemplate.opsForValue().set("test:key", "hello");
String value = redisTemplate.opsForValue().get("test:key");
assertThat(value).isEqualTo("hello");
}
}
四、共享容器
@Testcontainers
abstract class AbstractIntegrationTest {
@Container
static MySQLContainer<?> mysql = new MySQLContainer<>("mysql:8.0");
@Container
static GenericContainer<?> redis = new GenericContainer<>(
DockerImageName.parse("redis:7-alpine"))
.withExposedPorts(6379);
@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", mysql::getJdbcUrl);
registry.add("spring.datasource.username", mysql::getUsername);
registry.add("spring.datasource.password", mysql::getPassword);
registry.add("spring.redis.host", redis::getHost);
registry.add("spring.redis.port", () -> redis.getMappedPort(6379));
}
}
到此这篇关于SpringBoot整合Testcontainers实现数据库与中间件集成测试的文章就介绍到这了,更多相关SpringBoot Testcontainers集成测试内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
解决idea 中 SpringBoot 点击运行没反应按钮成灰色的问题
在使用 Spring Boot 开发项目时,可能会遇到一个问题:点击运行按钮后,控制台没有任何输出,项目界面也没有显示,这种情况可能是由多种原因导致的,本文将介绍一些常见的解决方法,需要的朋友可以参考下2023-08-08
Java的String类中的startsWith方法和endsWith方法示例详解
大家应该都知道startsWith()方法用于检测字符串是否以指定的前缀开始,endsWith()方法用于测试字符串是否以指定的后缀结束,本文就Java的String类中的startsWith方法和endsWith方法给大家详细讲解,感兴趣的朋友一起看看吧2023-11-11


最新评论