以下是一个简单的基于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/