zoukankan      html  css  js  c++  java
  • testcontainers 基本使用

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

  • 相关阅读:
    工作交接
    .NET 利用反射将对象数据添加到数据库
    【C#】IPAddress.Any 解决本地ip和服务器ip切换问题
    【C#】警告System.Configuration.ConfigurationSettings.AppSettings" 已过时
    【C#】TcpListener的对象“已过时”的编译警告
    大城小胖这几年积累的动画库、手势库、物理引擎库
    select option项选择后跳转页面
    360引起的Soap的java.io.EOFException错误
    linux安装mysql-5.7.22与数据自动备份
    性能测试流程
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/11970235.html
Copyright © 2011-2022 走看看