Back to Repositories

Testing Spring Data MongoDB Repository Operations in springboot-labs

This test suite demonstrates comprehensive testing of MongoDB operations using Spring Data MongoDB repository patterns. It validates basic CRUD operations and custom queries for user management functionality in a Spring Boot application.

Test Coverage Overview

The test suite provides thorough coverage of MongoDB repository operations, including insert, update, delete, and query functionalities.

Key areas tested include:
  • User document creation with nested Profile objects
  • Full document updates with save operations
  • Single and bulk record retrieval by IDs
  • Record deletion operations
  • Optional return type handling for null safety

Implementation Analysis

The testing approach utilizes Spring’s test framework with JUnit4 integration, leveraging @SpringBootTest for full application context loading. The implementation demonstrates proper dependency injection of repository components and uses Spring’s Assert utilities for validation.

Notable patterns include:
  • Spring Runner configuration for test execution
  • Autowired repository injection
  • Transaction-safe test methods
  • Proper handling of Optional return types

Technical Details

Testing infrastructure includes:
  • JUnit 4 test framework
  • Spring Test context framework
  • Spring Boot Test annotations
  • MongoDB test configuration
  • UserRepository interface extending MongoRepository
  • Custom data objects (UserDO) with embedded documents

Best Practices Demonstrated

The test suite exemplifies several testing best practices for MongoDB operations in Spring applications.

Notable practices include:
  • Isolated test methods for each operation type
  • Proper test method naming conventions
  • Comprehensive validation of repository operations
  • Clean separation of test scenarios
  • Proper exception handling and assertions

yudaocode/springboot-labs

lab-16-spring-data-mongo/lab-16-spring-data-mongodb/src/test/java/cn/iocoder/springboot/lab16/springdatamongodb/repository/UserRepositoryTest.java

            
package cn.iocoder.springboot.lab16.springdatamongodb.repository;

import cn.iocoder.springboot.lab16.springdatamongodb.Application;
import cn.iocoder.springboot.lab16.springdatamongodb.dataobject.UserDO;
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 org.springframework.util.Assert;

import java.util.Arrays;
import java.util.Date;
import java.util.Optional;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class UserRepositoryTest {

    @Autowired
    private UserRepository userRepository;

    @Test // 插入一条记录
    public void testInsert() {
        // 创建 UserDO 对象
        UserDO user = new UserDO();
        user.setId(1); // 这里先临时写死一个 ID 编号,后面演示自增 ID 的时候,在修改这块
        user.setUsername("yudaoyuanma");
        user.setPassword("buzhidao");
        user.setCreateTime(new Date());
        // 创建 Profile 对象
        UserDO.Profile profile = new UserDO.Profile();
        profile.setNickname("芋道源码");
        profile.setGender(1);
        user.setProfile(profile);
        // 存储到 DB
        userRepository.insert(user);
    }

    // 这里要注意,如果使用 save 方法来更新的话,必须是全量字段,否则其它字段会被覆盖。
    // 所以,这里仅仅是作为一个示例。
    @Test // 更新一条记录
    public void testUpdate() {
        // 查询用户
        Optional<UserDO> userResult = userRepository.findById(1);
        Assert.isTrue(userResult.isPresent(), "用户一定要存在");
        // 更新
        UserDO updateUser = userResult.get();
        updateUser.setUsername("yutou");
        userRepository.save(updateUser);
    }

    @Test // 根据 ID 编号,删除一条记录
    public void testDelete() {
        userRepository.deleteById(1);
    }

    @Test // 根据 ID 编号,查询一条记录
    public void testSelectById() {
        Optional<UserDO> userDO = userRepository.findById(1);
        System.out.println(userDO.isPresent());
    }

    @Test // 根据 ID 编号数组,查询多条记录
    public void testSelectByIds() {
        Iterable<UserDO> users = userRepository.findAllById(Arrays.asList(1, 4));
        users.forEach(System.out::println);
    }

}