对于单元测试,集成测试里,如果被测试的方法中使用到了redis,你需要去模拟一个单机环境的redis server,因为只有这样,你的测试才是客观的,即不会因为网络和其它因素影响你测试的准确性!
它的源码在github上,大家有兴趣可以去看看,非常精简,而且还提供了单机,集群,哨兵多种redis环境,完全可以满足我们的测试需要。
//implementation 'org.springframework.boot:spring-boot-starter-data-redis', //testimplementation 'com.github.kstyrc:embedded-redis:0.6',
package com.lind.springonetoone.mock; import org.springframework.stereotype.component; import redis.embedded.redisserver; import javax.annotation.postconstruct; import javax.annotation.predestroy; import java.io.ioexception; @component public class redisservermock { private redisserver redisserver; /** * 构造方法之后执行. * * @throws ioexception */ @postconstruct public void startredis() throws ioexception { redisserver = new redisserver(6379); redisserver.start(); } /** * 析构方法之后执行. */ @predestroy public void stopredis() { redisserver.stop(); } }
public class stringvaluetest extends basetest { @autowired redistemplate redistemplate; @test public void settest() throws exception { redistemplate.opsforvalue().set("ok", "test"); system.out.println( "settest:" + redistemplate.opsforvalue().get("ok") ); } }
对于内嵌redis就说到这到,下回有机会说一下内嵌的mongodb,它也是集成测试时不能缺少的组件!
如对本文有疑问, 点击进行留言回复!!
springmvc之ResponseBody响应json数据遇到的错误及解决
uni-app 后台升级 静默升级 uniapp 后台更新 静默更新 在线升级
SpringBoot多Module启动报错Could not transfer metadata
Hibernate项目报错:Cannot call sendError() after the response has been committed
网友评论