Back to Repositories

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

This test suite validates the User1Mapper functionality in a Spring Boot application with multiple datasources using MyBatis XML configuration. It covers core user management operations including insertion, querying, and updating user records with proper database interactions.

Test Coverage Overview

The test suite provides comprehensive coverage of User1Mapper operations.

Key areas tested include:
  • User insertion with gender enumeration
  • Bulk user retrieval and list handling
  • Single user retrieval by ID
  • User data updates with verification
Integration points focus on MyBatis mapper interactions and Spring Boot’s database connectivity.

Implementation Analysis

The testing approach utilizes Spring Boot’s test framework with JUnit 4 integration. The implementation leverages @SpringBootTest for full application context loading and @RunWith(SpringRunner.class) for Spring test execution.

Notable patterns include dependency injection of mapper interfaces, explicit assertion checking, and proper test isolation for database operations.

Technical Details

Testing tools and configuration:
  • JUnit 4 testing framework
  • Spring Boot Test context
  • MyBatis XML-based mapper configuration
  • Spring Runner for test execution
  • Autowired dependency injection
  • Assert statements for validation

Best Practices Demonstrated

The test suite demonstrates several testing best practices including proper test method isolation, clear naming conventions, and comprehensive assertion checking.

Notable practices include:
  • Individual test methods for specific operations
  • Proper exception handling
  • Clear test method naming
  • Verification of expected outcomes
  • Proper use of Spring Boot testing annotations

ityouknow/spring-boot-examples

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

            
package com.neo.mapper;

import java.util.List;

import com.neo.mapper.test1.User1Mapper;
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.model.User;
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())));
	}

}