Back to Repositories

Testing User Repository Operations in spring-boot-examples

This test suite validates the UserRepository functionality in a Spring Boot application, focusing on user data persistence and retrieval operations. The tests cover core database operations including saving users, finding by criteria, and deletion.

Test Coverage Overview

The test suite provides coverage for essential UserRepository operations including:
  • User creation and persistence
  • User lookup by username/email criteria
  • User deletion functionality
  • Date formatting and handling
The tests verify both positive scenarios and specific lookup conditions, ensuring reliable user data management.

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 @RunWith(SpringRunner.class) for Spring test execution.

The implementation demonstrates proper dependency injection of UserRepository and validates core repository operations in a single comprehensive test method.

Technical Details

Testing tools and configuration:
  • JUnit 4 testing framework
  • Spring Boot Test context
  • SpringRunner test executor
  • Autowired repository injection
  • Date formatting utilities
  • Assert statements for validation

Best Practices Demonstrated

The test suite exhibits several testing best practices:
  • Proper test context configuration
  • Clean and focused test methods
  • Effective use of dependency injection
  • Comprehensive operation verification
  • Clear test data setup
  • Proper cleanup through delete operations

ityouknow/spring-boot-examples

2.x/spring-boot-web/src/test/java/com/neo/model/UserRepositoryTests.java

            
package com.neo.model;

import java.text.DateFormat;
import java.util.Date;

import com.neo.repository.UserRepository;
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;


@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTests {

	@Autowired
	private UserRepository userRepository;

	@Test
	public void test() throws Exception {
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);        
		String formattedDate = dateFormat.format(date);
		
		userRepository.save(new User("aa1", "[email protected]", "aa", "aa123456",formattedDate));
		userRepository.save(new User("bb2", "[email protected]", "bb", "bb123456",formattedDate));
		userRepository.save(new User("cc3", "[email protected]", "cc", "cc123456",formattedDate));

//		Assert.assertEquals(9, userRepository.findAll().size());
		Assert.assertEquals("bb2", userRepository.findByUserNameOrEmail("bb", "xxx126.com").getNickName());
		userRepository.delete(userRepository.findByUserName("aa"));
	}

}