Back to Repositories

Testing UserMapper CRUD Operations in spring-boot-examples MyBatis Implementation

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

Test Coverage Overview

The test suite provides comprehensive coverage of core UserMapper operations including:

  • User insertion with validation of multiple records
  • Query operations for retrieving user data
  • Update functionality with verification of modified fields
  • Edge cases handling for null/empty result sets

Implementation Analysis

The testing approach utilizes Spring Boot’s testing framework with JUnit 4 integration. Tests are structured using @RunWith(SpringRunner.class) and @SpringBootTest annotations for proper Spring context loading. Each test method focuses on isolated functionality with clear assertions and state verification.

Technical Details

Key technical components include:

  • Spring Boot Test Context framework
  • JUnit 4 testing framework
  • MyBatis integration for database operations
  • Autowired dependency injection for UserMapper
  • Custom User model with UserSexEnum implementation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test method naming reflecting functionality under test
  • Proper test isolation and setup
  • Explicit assertions for result validation
  • Comprehensive CRUD operation coverage
  • Effective use of Spring Boot testing annotations

ityouknow/spring-boot-examples

2.x/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));

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

}