Back to Repositories

Testing MyBatis Annotation-Based User Operations in spring-boot-examples

This test suite validates the functionality of MyBatis annotations in a Spring Boot application, focusing on user entity operations. It demonstrates comprehensive testing of database CRUD operations using Spring Boot’s testing framework integrated with JUnit.

Test Coverage Overview

The test suite provides thorough coverage of core database operations through MyBatis annotations.

  • User insertion with multiple records validation
  • Query operations for retrieving user data
  • Update functionality with assertion checks
  • Integration with UserSexEnum for data integrity

Implementation Analysis

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

  • Autowired dependency injection for UserMapper
  • Structured test methods with clear naming conventions
  • Assert statements for validation

Technical Details

  • Spring Boot Test Context framework
  • JUnit 4 testing framework
  • MyBatis annotation-based mapping
  • Spring Runner for test execution
  • Autowired component injection
  • Assert utilities for test validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Spring Boot applications.

  • Isolated test methods for each operation
  • Clear test method naming
  • Proper assertion usage
  • Clean setup with Spring Boot test annotations
  • Consistent error handling patterns

ityouknow/spring-boot-examples

1.x/spring-boot-mybatis-annotation/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.entity.UserEntity;
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 UserEntity("aa", "a123456", UserSexEnum.MAN));
		UserMapper.insert(new UserEntity("bb", "b123456", UserSexEnum.WOMAN));
		UserMapper.insert(new UserEntity("cc", "b123456", UserSexEnum.WOMAN));

		Assert.assertEquals(3, UserMapper.getAll().size());
	}

	@Test
	public void testQuery() throws Exception {
		List<UserEntity> users = UserMapper.getAll();
		System.out.println(users.toString());
	}
	
	
	@Test
	public void testUpdate() throws Exception {
		UserEntity user = UserMapper.getOne(3l);
		System.out.println(user.toString());
		user.setNickName("neo");
		UserMapper.update(user);
		Assert.assertTrue(("neo".equals(UserMapper.getOne(3l).getNickName())));
	}

}