nosql-redis整合测试

2023-12-24 23:32:19

1、创建项目并导入redis

在这里插入图片描述

2、配置redis

在这里插入图片描述

3、写测试类

在这里插入图片描述

4、在redis中创建key

在这里插入图片描述

5、访问8082

在这里插入图片描述

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"));
    }

}

文章来源:https://blog.csdn.net/m0_50207524/article/details/135187624
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。