Back to Repositories

Testing MyBatis XML Multi-Datasource User Operations in spring-boot-examples

This test suite validates the User2Mapper functionality in a Spring Boot application with multiple datasources using MyBatis XML configuration. It covers essential CRUD operations and data persistence verification through systematic unit tests.

Test Coverage Overview

The test suite provides comprehensive coverage of User entity operations through the User2Mapper interface. Key functionality includes:

  • User insertion with gender enumeration validation
  • Bulk data retrieval and null checking
  • Single record updates with assertion verification
  • Edge cases handling for empty result sets

Implementation Analysis

The testing approach utilizes Spring Boot’s test framework integrated with JUnit 4. It employs the @SpringBootTest annotation for full application context loading and @RunWith(SpringRunner.class) for Spring test execution.

The implementation demonstrates dependency injection of the mapper interface and systematic validation of database operations through Assert statements.

Technical Details

  • Spring Boot Test Context framework
  • JUnit 4 testing framework
  • MyBatis XML-based mapper configuration
  • Multiple datasource setup
  • Custom User model with enum support
  • Automated assertion validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolated test methods, clear test method naming, and proper assertion usage. Each test focuses on a specific operation (insert, query, update) with appropriate setup and verification steps.

  • Proper test method isolation
  • Clear and descriptive test names
  • Robust assertion checking
  • Comprehensive CRUD operation coverage

ityouknow/spring-boot-examples

2.x/spring-boot-mybatis/spring-boot-mybatis-xml-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));

		Assert.assertEquals(3, 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(6l);
		System.out.println(user.toString());
		user.setNickName("neo");
		userMapper.update(user);
		Assert.assertTrue(("neo".equals(userMapper.getOne(6l).getNickName())));
	}

}