Back to Repositories

Testing Sharding-JDBC Database Operations in spring-boot-demo

This test suite validates the functionality of Sharding-JDBC integration with Spring Boot, focusing on database sharding operations. It demonstrates comprehensive testing of CRUD operations across sharded database tables.

Test Coverage Overview

The test suite provides extensive coverage of core Sharding-JDBC operations.

Key areas tested include:
  • Data insertion across multiple shards
  • Update operations with specific shard routing
  • Delete operations across sharded tables
  • Select queries with cross-shard data retrieval

Implementation Analysis

The testing approach utilizes Spring Boot Test framework with JUnit4 integration. It implements a systematic verification of order management operations using MyBatis-Plus as the ORM layer.

Technical patterns include:
  • Autowired dependency injection for data access
  • Lambda-based query construction
  • Randomized test data generation

Technical Details

Testing infrastructure includes:
  • SpringRunner for test execution
  • Hutool utility for random data generation
  • MyBatis-Plus for database operations
  • Lombok for reduced boilerplate
  • SLF4J for test logging

Best Practices Demonstrated

The test suite exemplifies several testing best practices for distributed databases.

Notable practices include:
  • Isolated test methods for each CRUD operation
  • Comprehensive shard routing validation
  • Clear test method documentation
  • Structured test data generation

xkcoding/spring-boot-demo

demo-sharding-jdbc/src/test/java/com/xkcoding/sharding/jdbc/SpringBootDemoShardingJdbcApplicationTests.java

            
package com.xkcoding.sharding.jdbc;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xkcoding.sharding.jdbc.mapper.OrderMapper;
import com.xkcoding.sharding.jdbc.model.Order;
import lombok.extern.slf4j.Slf4j;
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.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * <p>
 * 测试sharding-jdbc分库分表
 * </p>
 *
 * @author yangkai.shen
 * @date Created in 2019-03-26 13:44
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootDemoShardingJdbcApplicationTests {
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 测试新增
     */
    @Test
    public void testInsert() {
        for (long i = 1; i < 10; i++) {
            for (long j = 1; j < 20; j++) {
                Order order = Order.builder().userId(i).orderId(j).remark(RandomUtil.randomString(20)).build();
                orderMapper.insert(order);
            }
        }
    }

    /**
     * 测试更新
     */
    @Test
    public void testUpdate() {
        Order update = new Order();
        update.setRemark("修改备注信息");
        orderMapper.update(update, Wrappers.<Order>update().lambda().eq(Order::getOrderId, 2).eq(Order::getUserId, 2));
    }

    /**
     * 测试删除
     */
    @Test
    public void testDelete() {
        orderMapper.delete(new QueryWrapper<>());
    }

    /**
     * 测试查询
     */
    @Test
    public void testSelect() {
        List<Order> orders = orderMapper.selectList(Wrappers.<Order>query().lambda().in(Order::getOrderId, 1, 2));
        log.info("【orders】= {}", JSONUtil.toJsonStr(orders));
    }

}