Back to Repositories

Validating User Repository Operations in spring-boot-examples

This test suite validates the User Repository functionality in a Spring Boot application, focusing on basic CRUD operations and user data persistence. The tests verify user creation, retrieval by various criteria, and deletion operations using Spring’s testing framework.

Test Coverage Overview

The test suite provides comprehensive coverage of core User Repository operations.

Key areas tested include:
  • User creation and persistence
  • User retrieval by username and email
  • User deletion operations
  • Collection size verification
Integration points cover Spring Data JPA repository interactions and database operations.

Implementation Analysis

The testing approach utilizes Spring Boot’s testing framework with JUnit integration. The implementation leverages @RunWith(SpringRunner.class) and @SpringBootTest annotations for complete Spring context loading and dependency injection. The test demonstrates proper date formatting and user object creation patterns with multiple test scenarios.

Technical Details

Testing tools and configuration:
  • Spring Boot Test framework
  • JUnit 4 testing framework
  • Spring Runner for test execution
  • Autowired repository injection
  • DateFormat utility for timestamp handling
  • Assert statements for verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper test isolation, clear arrangement of test data, and comprehensive assertion checking. Notable practices include:
  • Proper dependency injection usage
  • Multiple assertion validations
  • Clear test data setup
  • Effective use of Spring Boot testing annotations

ityouknow/spring-boot-examples

1.x/spring-boot-web/src/test/java/com/neo/domain/UserRepositoryTests.java

            
package com.neo.domain;

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

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.SpringJUnit4ClassRunner;

import com.neo.Application;
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("bb", userRepository.findByUserNameOrEmail("bb", "[email protected]").getNickName());
		userRepository.delete(userRepository.findByUserName("aa1"));
	}

}