Back to Repositories

Testing MyBatis User Mapper Operations in spring-boot-demo

This test suite validates the UserMapper functionality in a Spring Boot MyBatis ORM implementation. It covers essential CRUD operations for user management, including retrieval, creation, and deletion of user records. The tests ensure robust database interactions and data integrity.

Test Coverage Overview

The test suite provides comprehensive coverage of UserMapper operations.

Key functionality tested includes:
  • Retrieving all users from the database
  • Fetching single user by ID
  • Creating new user records with password encryption
  • Deleting users by ID
Edge cases include validation of empty result sets and proper handling of user authentication data.

Implementation Analysis

The testing approach utilizes JUnit 4 framework with Spring Boot test integration. The implementation follows a systematic pattern of arranging test data, executing mapper operations, and asserting expected outcomes.

Notable features include:
  • Autowired dependency injection of UserMapper
  • Hutool utility usage for data generation
  • Secure password handling with salt generation
  • Lombok integration for cleaner test code

Technical Details

Testing tools and configuration:
  • JUnit 4 test framework
  • Spring Boot Test for dependency injection
  • Hutool for utility operations
  • Slf4j for test logging
  • MyBatis ORM framework
  • Assert statements for test validation

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through clear organization and thorough validation.

Notable practices include:
  • Descriptive test method names
  • Proper test isolation
  • Comprehensive assertions
  • Effective logging implementation
  • Clean and maintainable test structure

xkcoding/spring-boot-demo

demo-orm-mybatis/src/test/java/com/xkcoding/orm/mybatis/mapper/UserMapperTest.java

            
package com.xkcoding.orm.mybatis.mapper;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.xkcoding.orm.mybatis.SpringBootDemoOrmMybatisApplicationTests;
import com.xkcoding.orm.mybatis.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * UserMapper 测试类
 * </p>
 *
 * @author yangkai.shen
 * @date Created in 2018-11-08 11:25
 */
@Slf4j
public class UserMapperTest extends SpringBootDemoOrmMybatisApplicationTests {
    @Autowired
    private UserMapper userMapper;

    /**
     * 测试查询所有
     */
    @Test
    public void selectAllUser() {
        List<User> userList = userMapper.selectAllUser();
        Assert.assertTrue(CollUtil.isNotEmpty(userList));
        log.debug("【userList】= {}", userList);
    }

    /**
     * 测试根据主键查询单个
     */
    @Test
    public void selectUserById() {
        User user = userMapper.selectUserById(1L);
        Assert.assertNotNull(user);
        log.debug("【user】= {}", user);
    }

    /**
     * 测试保存
     */
    @Test
    public void saveUser() {
        String salt = IdUtil.fastSimpleUUID();
        User user = User.builder().name("testSave3").password(SecureUtil.md5("123456" + salt)).salt(salt).email("[email protected]").phoneNumber("17300000003").status(1).lastLoginTime(new DateTime()).createTime(new DateTime()).lastUpdateTime(new DateTime()).build();
        int i = userMapper.saveUser(user);
        Assert.assertEquals(1, i);
    }

    /**
     * 测试根据主键删除
     */
    @Test
    public void deleteById() {
        int i = userMapper.deleteById(1L);
        Assert.assertEquals(1, i);
    }
}