[Spring](EN) Test JPA Repository using @DataJpaTest

Test JPA Repository using @DataJpaTest


Environment and Prerequisite

  • Java
  • Spring
  • JUnit5
  • SpringExtension.class


@DataJpaTest

@DataJpaTest

  • This annotation applies only configuration relevant to JPA tests.
  • It will configure an in-memory embedded database, scan @Entity classes and configure Spring Data JPA repositories. Setting is rollbacked at the end of each test.


Example

build.gradle

plugins {
	id 'org.springframework.boot' version '2.5.13'
	id 'io.spring.dependency-management' version '1.0.11.RELEASE'
	id 'java'
}

group = 'org.twpower'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '8'

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

repositories {
	mavenCentral()
	maven { url 'https://repo.spring.io/milestone' }
	maven { url 'https://repo.spring.io/snapshot' }
}

dependencies {
	implementation('org.springframework.boot:spring-boot-starter-data-jpa')
	implementation('org.springframework.boot:spring-boot-starter-web')

	compileOnly 'org.projectlombok:lombok'
	annotationProcessor 'org.projectlombok:lombok'

	runtimeOnly 'com.h2database:h2'

	testImplementation('org.springframework.boot:spring-boot-starter-test')
	testImplementation('org.junit.jupiter:junit-jupiter')
	testImplementation('org.junit.jupiter:junit-jupiter-api')
	testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine')
}

test {
	useJUnitPlatform()
}

Entity

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = "email")})
public class UserEntity {
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    private String id;

    @Column(nullable = false)
    private String username;

    @Column(nullable = false)
    private String email;

    @Column(nullable = false)
    private String password;
}

JPA Repository

@Repository
public interface UserRepository extends JpaRepository<UserEntity, String> {
    UserEntity findByEmail(String email);
}

Test

@ExtendWith(SpringExtension.class)
@DataJpaTest
class UserRepositoryTest {

    @Autowired
    private UserRepository userRepository;

    @Test
    public void saveTest() {
        UserEntity userEntity = UserEntity.builder()
                .email("test@test.com")
                .username("testUsername")
                .password("testPassword")
                .build();

        UserEntity savedUserEntity = userRepository.save(userEntity);

        Assertions.assertEquals("test@test.com", savedUserEntity.getEmail());
        Assertions.assertEquals("testUsername", savedUserEntity.getUsername());
    }

    @Test
    public void findByEmailTest() {
        UserEntity userEntity = UserEntity.builder()
                .email("test@test.com")
                .username("testUsername")
                .password("testPassword")
                .build();
        UserEntity savedUserEntity = userRepository.save(userEntity);

        UserEntity foundUserEntity = userRepository.findByEmail(userEntity.getEmail());

        Assertions.assertEquals("test@test.com", savedUserEntity.getEmail());
        Assertions.assertEquals("test@test.com", foundUserEntity.getEmail());
    }
}


Reference