Back to Repositories

Testing MyBatis-Plus ActiveRecord Operations in spring-boot-demo

This test suite demonstrates comprehensive testing of MyBatis-Plus ActiveRecord functionality in a Spring Boot application. It validates core database operations including insertion, updates, queries, and deletions using the ActiveRecord pattern implementation.

Test Coverage Overview

The test suite provides thorough coverage of ActiveRecord operations in MyBatis-Plus:
  • CRUD operations testing with role entity
  • Query wrapper implementation validation
  • ID-based and condition-based operations
  • Assertion verification for all database operations

Implementation Analysis

The testing approach utilizes JUnit framework with MyBatis-Plus ActiveRecord pattern. Each test method focuses on a specific database operation, employing both direct ID-based methods and wrapper-based conditions for comprehensive validation.

The implementation leverages lambda expressions for type-safe queries and updates.

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • MyBatis-Plus ActiveRecord implementation
  • Lombok for logging capabilities
  • Spring Boot test configuration
  • QueryWrapper and UpdateWrapper utilities

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolated test methods for each operation type
  • Clear test method naming conventions
  • Proper assertion usage for validation
  • Comprehensive logging for debugging
  • Extension of base test class for shared configuration

xkcoding/spring-boot-demo

demo-orm-mybatis-plus/src/test/java/com/xkcoding/orm/mybatis/plus/activerecord/ActiveRecordTest.java

            
package com.xkcoding.orm.mybatis.plus.activerecord;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xkcoding.orm.mybatis.plus.SpringBootDemoOrmMybatisPlusApplicationTests;
import com.xkcoding.orm.mybatis.plus.entity.Role;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

/**
 * <p>
 * Role
 * </p>
 *
 * @author yangkai.shen
 * @date Created in 2019-09-14 14:19
 */
@Slf4j
public class ActiveRecordTest extends SpringBootDemoOrmMybatisPlusApplicationTests {
    /**
     * 测试 ActiveRecord 插入数据
     */
    @Test
    public void testActiveRecordInsert() {
        Role role = new Role();
        role.setName("VIP");
        Assert.assertTrue(role.insert());
        // 成功直接拿会写的 ID
        log.debug("【role】= {}", role);
    }

    /**
     * 测试 ActiveRecord 更新数据
     */
    @Test
    public void testActiveRecordUpdate() {
        Assert.assertTrue(new Role().setId(1L).setName("管理员-1").updateById());
        Assert.assertTrue(new Role().update(new UpdateWrapper<Role>().lambda().set(Role::getName, "普通用户-1").eq(Role::getId, 2)));
    }

    /**
     * 测试 ActiveRecord 查询数据
     */
    @Test
    public void testActiveRecordSelect() {
        Assert.assertEquals("管理员", new Role().setId(1L).selectById().getName());
        Role role = new Role().selectOne(new QueryWrapper<Role>().lambda().eq(Role::getId, 2));
        Assert.assertEquals("普通用户", role.getName());
        List<Role> roles = new Role().selectAll();
        Assert.assertTrue(roles.size() > 0);
        log.debug("【roles】= {}", roles);
    }

    /**
     * 测试 ActiveRecord 删除数据
     */
    @Test
    public void testActiveRecordDelete() {
        Assert.assertTrue(new Role().setId(1L).deleteById());
        Assert.assertTrue(new Role().delete(new QueryWrapper<Role>().lambda().eq(Role::getName, "普通用户")));
    }
}