Back to Repositories

Testing MyBatis XML Mapper Operations in spring-boot-examples

This test suite validates the UserMapper functionality in a Spring Boot MyBatis application, covering CRUD operations for user management. The tests ensure proper database interactions and data persistence using MyBatis XML mappings.

Test Coverage Overview

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

  • Tests user creation with different gender enums
  • Validates retrieval of all users from database
  • Verifies user update operations and persistence
  • Handles null checking and empty result sets

Implementation Analysis

The testing approach utilizes Spring Boot’s testing framework with JUnit integration. It employs the @SpringBootTest annotation for full application context loading and dependency injection.

The implementation follows a systematic pattern of arranging test data, executing mapper operations, and asserting expected outcomes using JUnit assertions.

Technical Details

  • Spring Runner for test execution (@RunWith(SpringRunner.class))
  • Autowired dependency injection for UserMapper
  • JUnit 4 testing framework
  • MyBatis XML-based mapper configuration
  • Spring Boot test context management

Best Practices Demonstrated

The test suite exemplifies several testing best practices for database integration testing.

  • Isolated test methods for different operations
  • Proper null checking and error handling
  • Clear test method naming conventions
  • Use of meaningful test data
  • Assertion-based result validation

ityouknow/spring-boot-examples

spring-boot-mybatis/spring-boot-mybatis-xml/src/test/java/com/neo/mapper/UserMapperTest.java

            
package com.neo.mapper;

import java.util.List;

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 UserMapperTest {

	@Autowired
	private UserMapper 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 {

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

}