Back to Repositories

Testing Redis Cache Integration with MyBatis Operations in SpringBoot-Labs

This test suite validates Redis caching functionality in a Spring Boot application, focusing on user data operations and cache management. The tests verify the integration between MyBatis mapper operations and Redis cache behavior for user entity CRUD operations.

Test Coverage Overview

The test suite provides comprehensive coverage of user data caching operations:

  • Cache manager initialization and configuration verification
  • User record retrieval with cache validation
  • New user insertion with cache population checks
  • User deletion with cache eviction verification

Implementation Analysis

The testing approach utilizes Spring Boot’s test framework with JUnit4 integration. It implements cache verification patterns using direct CacheManager access to validate cache states after operations. The tests employ SpringRunner for context loading and dependency injection.

Technical Details

  • Spring Boot Test framework with @SpringBootTest annotation
  • JUnit4 test runner and assertions
  • Redis cache implementation through CacheManager
  • MyBatis mapper integration for database operations
  • UUID generation for unique test data

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper test isolation, comprehensive cache state verification, and thorough CRUD operation coverage. Each test method focuses on a specific cache interaction scenario while maintaining independence through fresh data generation.

yudaocode/springboot-labs

lab-21/lab-21-cache-redis/src/test/java/cn/iocoder/springboot/lab21/cache/UserMapperTest.java

            
package cn.iocoder.springboot.lab21.cache;

import cn.iocoder.springboot.lab21.cache.dataobject.UserDO;
import cn.iocoder.springboot.lab21.cache.mapper.UserMapper;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.CacheManager;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.UUID;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class UserMapperTest {

    private static final String CACHE_NAME_USER = "users";

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CacheManager cacheManager;

    @Test
    public void testCacheManager() {
        System.out.println(cacheManager);
    }

    @Test
    public void testSelectById() {
        // 这里,胖友事先插入一条 id = 1 的记录。
        Integer id = 1;

        // 查询 id = 1 的记录
        UserDO user = userMapper.selectById(id);
        System.out.println("user:" + user);
        // 判断缓存中,是不是存在
        Assert.assertNotNull("缓存为空", cacheManager.getCache(CACHE_NAME_USER).get(user.getId(), UserDO.class));

        // 查询 id = 1 的记录
        user = userMapper.selectById(id);
        System.out.println("user:" + user);
    }

    @Test
    public void testInsert() {
        // 插入记录
        UserDO user = new UserDO();
        user.setUsername(UUID.randomUUID().toString()); // 随机账号,因为唯一索引
        user.setPassword("nicai");
        user.setCreateTime(new Date());
        user.setDeleted(0);
        userMapper.insert0(user);

        // 判断缓存中,是不是存在
        Assert.assertNotNull("缓存为空", cacheManager.getCache(CACHE_NAME_USER).get(user.getId(), UserDO.class));
    }

    @Test
    public void testDeleteById() {
        // 插入记录,为了让缓存里有记录
        UserDO user = new UserDO();
        user.setUsername(UUID.randomUUID().toString()); // 随机账号,因为唯一索引
        user.setPassword("nicai");
        user.setCreateTime(new Date());
        user.setDeleted(0);
        userMapper.insert0(user);
        // 判断缓存中,是不是存在
        Assert.assertNotNull("缓存为空", cacheManager.getCache(CACHE_NAME_USER).get(user.getId(), UserDO.class));

        // 删除记录,为了让缓存被删除
        userMapper.deleteById(user.getId());
        // 判断缓存中,是不是存在
        Assert.assertNull("缓存不为空", cacheManager.getCache(CACHE_NAME_USER).get(user.getId(), UserDO.class));
    }

}