基于SpringBoot集成测试远程连接的Redis服务
程序员文章站
2022-03-10 19:14:44
使用SpringBoot初始化项目,集成测试远程连接的Redis服务,并记录了五大基本数据类型,在jedis中的体现方式。 ......
前期准备
linux虚拟机或者租用的云服务器:sudo安装redis,或者docker加载redis镜像。后者需要使用docker启用redis容器。
配置好redis.conf文件。注意:一定要注释
# bind 127.0.0.1
其他详细配置可参考我另一篇文章,不过能想到集成测试redis,配置文件应该已经配置好了。
/sbin/iptables -i input -p tcp --dport 6379 -j accept
开放6379端口
初始化springboot项目
- 使用spring initializr 初始化一个springboot项目,可以勾选nosql下这两项,或者后面自行添加pom依赖。(当然也可以初始化一个maven项目来测试)
-
添加jedis依赖
<!--导入jedis依赖--> <dependency> <groupid>redis.clients</groupid> <artifactid>jedis</artifactid> <version>3.2.0</version> </dependency>
-
编写简易的单元测试程序
可使用
ifconfig
或者ip addr
查询服务器的ip地址/** * host: 虚拟机或远程服务器的ip地址 * port: redis.conf中配置的服务端口号 */ @test void testping() { jedis jedis = new jedis("192.168.137.132",6379); system.out.println("testping >>>>>>>>>>>>> "+jedis.ping()); }
测试结果:
测试五大基本数据类型
测试过程中,有些数据防止重复插入,部分测试用例使用了
del
命令。
-
五大基本数据类型:字符串,列表,哈希,集合,有序集合
-
每次单元测试都需进行远程连接,故做以下处理:
@springboottest class jedisdemoapplicationtests { // 此处配置连接 jedis jedis = new jedis("192.168.137.132",6379); @test void contextloads() { } // 此处进行单元测试 }
key键
测试用例:
@test void keytest() { system.out.println(jedis.keys("*")); system.out.println(jedis.keys("*").getclass()); }
测试结果:
[list01, customer, set01, username] class java.util.hashset
string字符串
测试用例:
@test void stringtest() { system.out.println("测试set >>> "+jedis.set("name", "kris")); system.out.println("测试get >>> "+jedis.get("name")); system.out.println(jedis.get("name").getclass()); system.out.println("\n测试append >>> "+jedis.append("name", " ^v^")); system.out.println("测试strlen >>> "+jedis.strlen("name")); system.out.println("测试get >>> "+jedis.get("name")); system.out.println("\n测试mset >>> "+jedis.mset( "username", "kris", "age", "18", "sex","男")); system.out.println("测试mget >>> "+jedis.mget("username", "age", "sex")); system.out.println("\n测试incr >>> "+jedis.incr("age")); system.out.println("测试incrby >>> "+jedis.incrby("age",3)); system.out.println("测试decr >>> "+jedis.decr("age")); system.out.println("测试decrby >>> "+jedis.decrby("age", 3)); }
测试结果:
测试set >>> ok 测试get >>> kris class java.lang.string 测试append >>> 8 测试strlen >>> 8 测试get >>> kris ^v^ 测试mset >>> ok 测试mget >>> [kris, 18, 男] 测试incr >>> 19 测试incrby >>> 22 测试decr >>> 21 测试decrby >>> 18
list列表
测试用例:
@test void listtest() { system.out.println("测试rpush >>> "+jedis.rpush("list01", "i", "love", "redis", "!")); system.out.println("测试lpush >>> "+jedis.lpush("list01", "hello")); system.out.println("测试lrange >>> "+jedis.lrange("list01", 0, -1)); system.out.println(jedis.lrange("list01", 0, -1).getclass()); system.out.println("\n测试lpop >>> "+jedis.lpop("list01")); system.out.println("测试rpop >>> "+jedis.rpop("list01")); system.out.println("测试lrange >>> "+jedis.lrange("list01", 0, -1)); system.out.println("\n测试lset >>> "+jedis.lset("list01", 0, "we")); system.out.println("测试lrange >>> "+jedis.lrange("list01", 0, -1)); system.out.println("\n测试 linsert >>> "+jedis.linsert("list01", listposition.after, "redis", "!")); system.out.println("测试lrange >>> "+jedis.lrange("list01", 0, -1)); jedis.del("list01"); }
测试结果:
测试rpush >>> 4 测试lpush >>> 5 测试lrange >>> [hello, i, love, redis, !] class java.util.arraylist 测试lpop >>> hello 测试rpop >>> ! 测试lrange >>> [i, love, redis] 测试lset >>> ok 测试lrange >>> [we, love, redis] 测试 linsert >>> 4 测试lrange >>> [we, love, redis, !]
hash哈希
测试用例:
@test void hashtest() { map<string, string> customer = new hashmap<>(); customer.put("username", "kris"); customer.put("password", "123456"); customer.put("email", "kris@163.com"); system.out.println("测试hset >>> "+jedis.hset("customer", customer)); system.out.println("测试hget >>> "+jedis.hget("customer", "email")); system.out.println("测试getall >>> "+jedis.hgetall("customer")); system.out.println(jedis.hgetall("customer").getclass()); }
测试结果:
测试hset >>> 0 测试hget >>> kris@163.com 测试getall >>> {name=kris, email=kris@163.com, password=123456, username=kris} class java.util.hashmap
set集合
测试用例:
@test void settest() { system.out.println("测试sadd >>> "+jedis.sadd("names", "louis", "kris", "jerry", "jackson")); system.out.println("测试smembers >>> "+jedis.smembers("names")); system.out.println("测试scard >>> "+jedis.scard("names")); system.out.println(jedis.smembers("names").getclass()); system.out.println("\n测试srandmember >>> "+jedis.srandmember("names", 3)); system.out.println("测试srandmember >>> "+jedis.srandmember("names", 3)); system.out.println("\n测试spop >>> "+jedis.spop("names", 2)); system.out.println("测试smembers >>> "+jedis.smembers("names")); system.out.println("\n测试sadd >>> "+jedis.sadd("names0", "jack", "louis", "kris")); system.out.println("测试sunion >>> "+jedis.sunion("names", "names0")); system.out.println("测试sdiff >>> "+jedis.sdiff("names", "names0")); system.out.println("测试sinter >>> "+jedis.sinter("names", "names0")); system.out.println("测试smembers >>> "+jedis.smembers("names")); system.out.println("测试smembers >>> "+jedis.smembers("names0")); jedis.del("names", "names0"); }
测试结果:
测试sadd >>> 4 测试smembers >>> [kris, jerry, jackson, louis] 测试scard >>> 4 class redis.clients.jedis.binaryjedis$setfromlist 测试srandmember >>> [kris, jerry, jackson] 测试srandmember >>> [jerry, louis, jackson] 测试spop >>> [jerry, kris] 测试smembers >>> [jackson, louis] 测试sadd >>> 3 测试sunion >>> [jack, kris, louis, jackson] 测试sdiff >>> [jackson] 测试sinter >>> [louis] 测试smembers >>> [jackson, louis] 测试smembers >>> [kris, jack, louis]
zset有序集合
测试用例:
@test void sortsettest() { map<string, double> score = new hashmap<>(); score.put("钻石", 1000.0); score.put("王者", 2000.0); // 无序 score.put("星耀", 1500.0); score.put("荣耀", 2500.0); system.out.println("测试zadd >>> "+jedis.zadd("score", score)); system.out.println("测试zrangebyscore >>> "+jedis.zrangebyscore("score", 1500,2500)); system.out.println("测试zrange >>> "+jedis.zrange("score", 0,-1)); system.out.println("测试zrevrange >>> "+jedis.zrevrange("score", 0,-1)); system.out.println(jedis.smembers("names").getclass()); jedis.del("score"); }
测试结果:
测试zadd >>> 4 测试zrangebyscore >>> [星耀, 王者, 荣耀] 测试zrange >>> [钻石, 星耀, 王者, 荣耀] 测试zrevrange >>> [荣耀, 王者, 星耀, 钻石] class redis.clients.jedis.binaryjedis$setfromlist
下一篇: 线程池池