testcontainers 基本使用
2024-09-08 10:01:24
以下是一个简单的基于testcontainers 的测试(基于java 项目)
预备环境
因为testcontainers 基于docker 运行,所以需要安装docker 引擎
项目准备
- 项目结构
├── README.md
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── dalong
│ │ ├── Cache.java
│ │ └── RedisBackedCache.java
│ └── resources
└── test
└── java
└── RedisTest.java
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dalong</groupId>
<artifactId>redit-test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<encoding>UTF-8</encoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.12.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.rnorth.visible-assertions</groupId>
<artifactId>visible-assertions</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
</dependencies>
</project>
- 代码说明
main 为主要实现,一个cache接口定义cache 契约,RedisBackedCache 为基于redis 的实现,RedisTest 为基于testcontainers 的测试用例
以下主要说明RedisTest.java 代码(基于junit 的测试)
import com.dalong.Cache;
import com.dalong.RedisBackedCache;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.testcontainers.containers.GenericContainer;
import redis.clients.jedis.Jedis;
import java.util.Optional;
import static org.rnorth.visibleassertions.VisibleAssertions.*;
public class RedisTest {
// 核心使用GenericContainer 模拟redis 服务,同时使用了固定端口,使用jedis 访问
@Rule
public GenericContainer redis = new GenericContainer("redis:3.0.6")
.withExposedPorts(6379);
private Cache cache;
@Before
public void setUp() throws Exception {
Jedis jedis = new Jedis(redis.getContainerIpAddress(), redis.getMappedPort(6379));
cache = new RedisBackedCache(jedis, "test");
}
@Test
public void testFindingAnInsertedValue() {
cache.put("foo", "FOO");
Optional<String> foundObject = cache.get("foo", String.class);
assertTrue("When an object in the cache is retrieved, it can be found",
foundObject.isPresent());
assertEquals("When we put a String in to the cache and retrieve it, the value is the same",
"FOO",
foundObject.get());
}
@Test
public void testNotFindingAValueThatWasNotInserted() {
Optional<String> foundObject = cache.get("bar", String.class);
assertFalse("When an object that's not in the cache is retrieved, nothing is found",
foundObject.isPresent());
}
}
说明
使用testcontainers 我们可以进行实际数据库的测试(数据库特性、版本)有一篇关于不要使用内存数据库mock 的
文章可以看看和嗯不错 https://phauer.com/2017/dont-use-in-memory-databases-tests-h2/
参考资料
https://github.com/rongfengliang/testcontainer-learning
https://github.com/testcontainers/testcontainers-java
https://testcontainers.org/
https://phauer.com/2017/dont-use-in-memory-databases-tests-h2/
最新文章
- IOS 开发 ARC兼容MRC框架
- easyui datagird 列宽自适应
- Web API 使用上安全吗?
- MySQL server has gone away的解决方法
- 【转】PHP框架性能测试报告 - ThinkPHP 3.2.3 Laravel 5.2 Yii2.0.5
- vim 实现 go to definition的功能
- [未完成][Mooc]关于Linxu的总结(一)
- Coder-Strike 2014 - Round 1(A~E)
- hdu 2203
- [ An Ac a Day ^_^ ] hdu 2553 N皇后问题 搜索
- Angular企业级开发(10)-Smart Table插件开发
- DevExtreme 学习应用[3]
- Xamarin 学习笔记 - Page(页面)
- vue中@contextmenu在pc和mac中的区别
- windows上react-native run-android时Exception in thread ";main"; java.lang.IllegalArgumentException: MALFORMED报错
- 蓝桥杯近3年初赛题之三(17年b组)
- DAY16 模块和包的导入
- MT【226】费马点两题
- JaveWeb 公司项目(3)----- 通过Thrift端口获取数据库数据
- Git的撤销与回滚
热门文章
- Pycharm2019.2.4专业版激活
- iOS Workflow 分享 - Debug Action
- 物联网架构成长之路(33)-EMQ数据存储到influxDB
- centos 文本文件编码转换
- CompletableFuture3
- virsh console hangs at the escape character “^]”
- WPF Button IsMouseOver Background
- C 结构体、位域
- django中视图函数中装饰器
- 在QT中使用FFmpeg库的部分报错问题