nosql-redis整合测试

nosql-redis整合测试

  • 1、创建项目并导入redis
  • 2、配置redis
  • 3、写测试类
  • 4、在redis中创建key
  • 5、访问8082
  • 6、在集成测试中测试方法

1、创建项目并导入redis

nosql-redis整合测试_第1张图片

2、配置redis

nosql-redis整合测试_第2张图片

3、写测试类

nosql-redis整合测试_第3张图片

4、在redis中创建key

nosql-redis整合测试_第4张图片

5、访问8082

nosql-redis整合测试_第5张图片

6、在集成测试中测试方法

package com.example.boot3.redis;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;

import java.util.UUID;

@SpringBootTest
class Boot307RedisApplicationTests {
    @Autowired
    StringRedisTemplate redisTemplate;

    //    常见的数据类型 k:v value可以有很多类型
    //    string: 普通字符串: redisTemplate.opsForValue()
    //    list: 列表: redisTemplate.opsForList()
    //    set: 集合: redisTemplate.opsForSet()
    //    zset: 有序集合: redisTemplate.opsForZSet()
    //    hash: map结构: redisTemplate.opsForHash()
    @Test
    void contextLoads() {
        redisTemplate.opsForValue().set("haha", UUID.randomUUID().toString());
        String haha = redisTemplate.opsForValue().get("haha");
        System.out.println(haha);
    }

    @Test
    void testList(){
        String listName = "listtest";
        redisTemplate.opsForList().leftPush(listName,"1");
        redisTemplate.opsForList().leftPush(listName,"2");
        redisTemplate.opsForList().leftPush(listName,"3");
        String pop = redisTemplate.opsForList().leftPop(listName);
        Assertions.assertEquals("3",pop);
    }

    @Test
    void testSet(){
        String setName = "setest";
        redisTemplate.opsForSet().add(setName,"1","2");
        Boolean aBoolean = redisTemplate.opsForSet().isMember(setName,"2");
        Assertions.assertTrue(aBoolean);
        Boolean aBoolean1 = redisTemplate.opsForSet().isMember(setName, "5");
        Assertions.assertFalse(aBoolean1);

    }

    @Test
    void testZSet(){
        String setName = "zsetest";
        redisTemplate.opsForZSet().add(setName,"类放羊",90.00);
        redisTemplate.opsForZSet().add(setName,"张三",91.00);
        redisTemplate.opsForZSet().add(setName,"李四",91.20);
        redisTemplate.opsForZSet().add(setName,"王五",92.00);
        ZSetOperations.TypedTuple<String> popMax = redisTemplate.opsForZSet().popMax(setName);
        String value = popMax.getValue();
        Double score = popMax.getScore();
        System.out.println(value + "==>" + score);
    }

    @Test
    void testHash(){
        String mapName = "amap";
        redisTemplate.opsForHash().put(mapName,"name","张三");
        redisTemplate.opsForHash().put(mapName,"age","18");

        System.out.println(redisTemplate.opsForHash().get(mapName,"name"));
        System.out.println(redisTemplate.opsForHash().get(mapName,"age"));
    }

}

你可能感兴趣的:(springboot,nosql,redis,数据库)