Back to Repositories

Testing MyBatis Multi-Datasource User Mapper Implementation in spring-boot-examples

This test suite validates the User2Mapper functionality in a Spring Boot application with multiple datasources using MyBatis annotations. It covers essential CRUD operations for user management with comprehensive test cases for data persistence and retrieval.

Test Coverage Overview

The test suite provides comprehensive coverage of User entity operations through MyBatis mapper interface.

Key areas tested include:
  • User insertion with enum handling for gender
  • Bulk user retrieval and null checks
  • Single user updates with assertion validation
  • Integration with multiple datasource configuration

Implementation Analysis

The testing approach utilizes Spring Boot’s test framework with JUnit 4 integration. The implementation leverages Spring’s dependency injection for mapper autowiring and employs the SpringRunner test executor.

Notable patterns include:
  • Annotation-based test configuration
  • Direct mapper interface testing
  • Transaction management for data operations

Technical Details

Testing stack includes:
  • JUnit 4 testing framework
  • Spring Boot Test context
  • MyBatis annotation-based mapping
  • Spring Runner for test execution
  • Assert utilities for validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test methods for specific functionality
  • Proper test method naming conventions
  • Comprehensive assertion checking
  • Clean separation of concerns between test cases
  • Effective use of Spring Boot test annotations

ityouknow/spring-boot-examples

spring-boot-mybatis/spring-boot-mybatis-annotation-mulidatasource/src/test/java/com/neo/mapper/User2MapperTest.java

            
package com.neo.mapper;

import com.neo.model.User;
import com.neo.enums.UserSexEnum;
import com.neo.mapper.test2.User2Mapper;
import org.junit.Assert;
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
public class User2MapperTest {

	@Autowired
	private User2Mapper userMapper;

	@Test
	public void testInsert() throws Exception {
		userMapper.insert(new User("aa", "a123456", UserSexEnum.MAN));
		userMapper.insert(new User("bb", "b123456", UserSexEnum.WOMAN));
		userMapper.insert(new User("cc", "b123456", UserSexEnum.WOMAN));
		System.out.println(userMapper.getAll().size());
	}

	@Test
	public void testQuery() throws Exception {
		List<User> users = userMapper.getAll();
		if(users==null || users.size()==0){
			System.out.println("is null");
		}else{
			System.out.println(users.toString());
		}
	}
	
	
	@Test
	public void testUpdate() throws Exception {
		User user = userMapper.getOne(30l);
		System.out.println(user.toString());
		user.setNickName("neo");
		userMapper.update(user);
		Assert.assertTrue(("neo".equals(userMapper.getOne(30l).getNickName())));
	}

}