首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Redis的单元测试 - Redis mock

Redis的单元测试 - Redis mock

作者头像
十毛
发布2019-04-17 17:22:17
9.6K0
发布2019-04-17 17:22:17
举报

设计到Redis的单元测试是比较麻烦的,一般使用embeded-redis或redis-mock。因为embeded-redis启动会失败,而且没有看到错误日志(只有Can't start redis server. Check logs for details),所以最后选择了redis-mock。

添加依赖pom.xml


添加依赖项:redis-mock

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>ai.grakn</groupId>
        <artifactId>redis-mock</artifactId>
        <version>0.1.6</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Redis使用代码RedisTestApplication.java

@SpringBootApplication
public class RedisTestApplication {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public static void main(String[] args) {
        SpringApplication.run(RedisTestApplication.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {
            stringRedisTemplate.opsForValue().set("name", "tenmao");
            String name = stringRedisTemplate.opsForValue().get("name");
            System.out.println(name);
        };
    }
}

Redis测试代码

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTestApplicationTests {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private static RedisServer redisServer = null;
    @BeforeClass
    public static void init() {
        try {
            redisServer = RedisServer.newRedisServer();
            redisServer.start();
            URL resource = RedisTestApplicationTests.class.getClassLoader().getResource("application.properties");
            Files.write("spring.redis.port=" + redisServer.getBindPort(), new File(resource.getFile()), StandardCharsets.UTF_8);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @AfterClass
    public static void close() {
        if (redisServer != null) {
            redisServer.stop();
        }
    }

    @Test
    public void testRedis() {
        String name = stringRedisTemplate.opsForValue().get("name");
        Assert.assertEquals("tenmao", name);
    }
}

常见问题

  • 因为RedisServer启动的端口无法预知(如果写死的话就容易有端口冲突),所以需要实现动态端口配置。参考了很多的资料,也没找到很好的解决办法,最后只能临时使用写文件的办法
URL resource = RedisTestApplicationTests.class.getClassLoader().getResource("application.properties");
Files.write("spring.redis.port=" + redisServer.getBindPort(), new File(resource.getFile()), StandardCharsets.UTF_8);

ps: 为了把文件写对地方,需要在建立test/resources/application.properties空文件

参考

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.04.03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 添加依赖pom.xml
  • Redis使用代码RedisTestApplication.java
  • Redis测试代码
  • 常见问题
  • 参考
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档