Back to Repositories

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

This test suite validates the functionality of User1Mapper in a Spring Boot application with multiple datasources using MyBatis annotations. It covers essential CRUD operations and data persistence verification across different user scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of User entity operations including:
  • User insertion with gender enumeration validation
  • Bulk data retrieval and null checks
  • Single user updates with field-level verification
  • Integration with MyBatis annotations for database operations

Implementation Analysis

The testing approach utilizes Spring Boot’s test framework with JUnit 4 integration. It employs @SpringBootTest for full application context loading and @RunWith(SpringRunner.class) for Spring test execution. The implementation leverages dependency injection for mapper access and includes assertion-based verification patterns.

Technical Details

Key technical components include:
  • Spring Boot Test Context framework
  • JUnit 4 testing framework
  • MyBatis annotation-based mapping
  • Spring dependency injection
  • Custom User model with enumerated types
  • Assert statements 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 usage
  • Null checking and error handling
  • Clean separation of concerns between test cases

ityouknow/spring-boot-examples

2.x/spring-boot-mybatis/spring-boot-mybatis-annotation-mulidatasource/src/test/java/com/neo/mapper/User1MapperTest.java

            
package com.neo.mapper;

import java.util.List;

import com.neo.mapper.test1.User1Mapper;
import com.neo.model.User;
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 com.neo.enums.UserSexEnum;

@RunWith(SpringRunner.class)
@SpringBootTest
public class User1MapperTest {

	@Autowired
	private User1Mapper 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.size());
		}
	}
	
	
	@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())));
	}

}