Back to Repositories

Testing User1Mapper Operations with MyBatis XML Multi-Datasource 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 essential CRUD operations and user management functionality through comprehensive unit tests.

Test Coverage Overview

The test suite provides comprehensive coverage of User1Mapper operations including insert, query, and update functionality.

  • Tests user insertion with different gender enums
  • Validates retrieval of all users
  • Verifies user update operations with assertion checks
  • Handles null checks and empty result sets

Implementation Analysis

The implementation follows Spring Boot testing best practices using JUnit4 and SpringRunner.

Key patterns include:
  • Autowired dependency injection for mapper access
  • Isolated test methods for each CRUD operation
  • Use of UserSexEnum for type-safe gender representation
  • Integration with Spring Boot test context

Technical Details

Testing infrastructure includes:

  • @SpringBootTest for full application context
  • JUnit4 test framework
  • Spring Test Context framework
  • MyBatis XML-based mapper configuration
  • Multiple datasource support

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test method naming conventions
  • Proper test isolation and setup
  • Effective use of assertions for validation
  • Comprehensive error checking
  • Integration with Spring Boot test infrastructure

ityouknow/spring-boot-examples

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));

		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.size());
		}
	}
	
	
	@Test
	public void testUpdate() throws Exception {
		Long id =30l;
		User user = userMapper.getOne(id);
		System.out.println(user.toString());
		user.setNickName("neo");
		userMapper.update(user);
		Assert.assertTrue(("neo".equals(userMapper.getOne(id).getNickName())));
	}

}