Back to Repositories

Testing Multiple JPA Repository Operations in spring-boot-examples

This test suite validates multiple JPA repository operations across two separate user repositories in a Spring Boot application. It demonstrates comprehensive testing of CRUD operations and basic queries using JUnit and Spring Boot Test framework.

Test Coverage Overview

The test suite provides coverage for essential JPA repository operations including:

  • User creation and persistence across multiple repositories
  • Bulk deletion operations
  • Basic query operations including findAll, findById, and count
  • Transaction management and data consistency

Implementation Analysis

The testing approach utilizes Spring Boot’s testing framework with JUnit integration. It employs @RunWith(SpringRunner.class) and @SpringBootTest annotations for comprehensive integration testing of JPA repositories. The implementation demonstrates parallel testing of multiple repositories using @Resource injection.

Technical Details

Key technical components include:

  • Spring Boot Test framework for context loading
  • JUnit 4 testing framework
  • Multiple JPA repositories (UserTest1Repository and UserTest2Repository)
  • @Transactional annotation for test isolation
  • DateFormat utility for timestamp handling

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test isolation and cleanup
  • Comprehensive CRUD operation testing
  • Clear test method naming conventions
  • Efficient resource injection using @Resource
  • Structured test organization with separate test methods for different operations

ityouknow/spring-boot-examples

2.x/spring-boot-jpa/spring-boot-multi-Jpa/src/test/java/com/neo/repository/UserRepositoryTests.java

            
package com.neo.repository;

import com.neo.model.User;
import com.neo.repository.test1.UserTest1Repository;
import com.neo.repository.test2.UserTest2Repository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTests {
	@Resource
	private UserTest1Repository userTest1Repository;
	@Resource
	private UserTest2Repository userTest2Repository;

	@Test
	public void testSave() throws Exception {
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
		String formattedDate = dateFormat.format(date);

		userTest1Repository.save(new User("aa", "aa123456","[email protected]", "aa",  formattedDate));
		userTest1Repository.save(new User("bb", "bb123456","[email protected]", "bb",  formattedDate));
		userTest2Repository.save(new User("cc", "cc123456","[email protected]", "cc",  formattedDate));
	}


	@Test
	public void testDelete() throws Exception {
		userTest1Repository.deleteAll();
		userTest2Repository.deleteAll();
	}

	@Test
	public void testBaseQuery() {
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
		String formattedDate = dateFormat.format(date);
		User user=new User("ff", "ff123456","[email protected]", "ff",  formattedDate);
		userTest1Repository.findAll();
		userTest2Repository.findById(3l);
		userTest2Repository.save(user);
		user.setId(2l);
		userTest1Repository.delete(user);
		userTest1Repository.count();
		userTest2Repository.findById(3l);
	}


}