Back to Repositories

Testing Multi-Datasource JPA Operations in spring-boot-demo

This test suite demonstrates the implementation of multiple database configurations in a Spring Boot application using JPA. It validates CRUD operations across primary and secondary datasources while ensuring data consistency and proper repository interactions.

Test Coverage Overview

The test suite provides comprehensive coverage of dual-database operations focusing on CRUD functionality.

  • Tests basic insert operations across both databases
  • Validates update synchronization between primary and secondary tables
  • Confirms proper deletion across multiple datasources
  • Verifies select operations and data retrieval from both repositories

Implementation Analysis

The testing approach utilizes Spring’s test framework with JUnit4 integration. It employs dependency injection for repository access and implements Hutool’s BeanUtil for object property mapping between database entities. The Snowflake ID generation ensures unique record identification across databases.

Technical Details

  • JUnit4 with SpringRunner for test execution
  • Spring Boot Test context configuration
  • Hutool utility library for bean operations
  • Snowflake ID generation strategy
  • JPA repositories for database operations
  • Slf4j for test logging

Best Practices Demonstrated

The test suite exemplifies several testing best practices for multi-datasource applications.

  • Clear separation of concerns between datasources
  • Proper test method isolation
  • Consistent entity mapping patterns
  • Comprehensive CRUD operation validation
  • Effective use of dependency injection

xkcoding/spring-boot-demo

demo-multi-datasource-jpa/src/test/java/com/xkcoding/multi/datasource/jpa/SpringBootDemoMultiDatasourceJpaApplicationTests.java

            
package com.xkcoding.multi.datasource.jpa;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import com.xkcoding.multi.datasource.jpa.entity.primary.PrimaryMultiTable;
import com.xkcoding.multi.datasource.jpa.entity.second.SecondMultiTable;
import com.xkcoding.multi.datasource.jpa.repository.primary.PrimaryMultiTableRepository;
import com.xkcoding.multi.datasource.jpa.repository.second.SecondMultiTableRepository;
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;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class SpringBootDemoMultiDatasourceJpaApplicationTests {
    @Autowired
    private PrimaryMultiTableRepository primaryRepo;
    @Autowired
    private SecondMultiTableRepository secondRepo;
    @Autowired
    private Snowflake snowflake;

    @Test
    public void testInsert() {
        PrimaryMultiTable primary = new PrimaryMultiTable(snowflake.nextId(), "测试名称-1");
        primaryRepo.save(primary);

        SecondMultiTable second = new SecondMultiTable();
        BeanUtil.copyProperties(primary, second);
        secondRepo.save(second);
    }

    @Test
    public void testUpdate() {
        primaryRepo.findAll().forEach(primary -> {
            primary.setName("修改后的" + primary.getName());
            primaryRepo.save(primary);

            SecondMultiTable second = new SecondMultiTable();
            BeanUtil.copyProperties(primary, second);
            secondRepo.save(second);
        });
    }

    @Test
    public void testDelete() {
        primaryRepo.deleteAll();

        secondRepo.deleteAll();
    }

    @Test
    public void testSelect() {
        List<PrimaryMultiTable> primary = primaryRepo.findAll();
        log.info("【primary】= {}", primary);

        List<SecondMultiTable> second = secondRepo.findAll();
        log.info("【second】= {}", second);
    }

}