Back to Repositories

Testing Multi-Database MongoDB Integration in spring-boot-examples

This test suite validates multi-database MongoDB integration in a Spring Boot application. It demonstrates the configuration and interaction with two separate MongoDB databases through Spring Data repositories, ensuring proper data persistence and retrieval functionality.

Test Coverage Overview

The test suite covers essential MongoDB multi-database operations:
  • Database connection and repository autowiring for both primary and secondary databases
  • Data persistence operations using save() method
  • Data retrieval operations using findAll() method
  • Verification of independent database operations

Implementation Analysis

The testing approach utilizes Spring Boot’s test framework with JUnit integration. It implements @SpringBootTest for full application context loading and @RunWith(SpringRunner.class) for Spring test execution. The test demonstrates repository pattern implementation with distinct primary and secondary database configurations.

Technical Details

Testing tools and configuration:
  • Spring Boot Test framework
  • JUnit 4 testing framework
  • Spring Data MongoDB
  • Autowired repository injection
  • Multiple MongoDB database configurations

Best Practices Demonstrated

The test exhibits several testing best practices:
  • Clear separation of concerns between primary and secondary databases
  • Proper dependency injection using @Autowired
  • Integration test scope definition using @SpringBootTest
  • Structured test execution with setup and verification phases

ityouknow/spring-boot-examples

1.x/spring-boot-multi-mongodb/src/test/java/com/neo/model/repository/MuliDatabaseTest.java

            
package com.neo.model.repository;

import com.neo.model.repository.primary.PrimaryMongoObject;
import com.neo.model.repository.primary.PrimaryRepository;
import com.neo.model.repository.secondary.SecondaryMongoObject;
import com.neo.model.repository.secondary.SecondaryRepository;
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;

/**
 * Created by neo on 2017/5/6.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class MuliDatabaseTest {

    @Autowired
    private PrimaryRepository primaryRepository;

    @Autowired
    private SecondaryRepository secondaryRepository;

    @Test
    public void TestSave() {

        System.out.println("************************************************************");
        System.out.println("测试开始");
        System.out.println("************************************************************");

        this.primaryRepository
                .save(new PrimaryMongoObject(null, "第一个库的对象"));

        this.secondaryRepository
                .save(new SecondaryMongoObject(null, "第二个库的对象"));

        List<PrimaryMongoObject> primaries = this.primaryRepository.findAll();
        for (PrimaryMongoObject primary : primaries) {
            System.out.println(primary.toString());
        }

        List<SecondaryMongoObject> secondaries = this.secondaryRepository.findAll();

        for (SecondaryMongoObject secondary : secondaries) {
            System.out.println(secondary.toString());
        }

        System.out.println("************************************************************");
        System.out.println("测试完成");
        System.out.println("************************************************************");
    }

}