[Spring] @DataJpaTest를 사용해 JPA Repository 테스트하기
@DataJpaTest를 사용해 JPA Repository를 테스트 해보자
환경
- Java
- Spring
- JUnit5
- SpringExtension.class
@DataJpaTest
@DataJpaTest
- JPA에 관련된 요소들만 테스트하기 위한 어노테이션으로 JPA 테스트에 관련된 설정들만 적용해준다.
- 메모리상에 내부 데이터베이스를 생성하고
@Entity
클래스들을 등록하고 JPA Repository 설정들을 해준다. 각 테스트마다 테스트가 완료되면 관련한 설정들은 롤백된다.
예제
build.gradle
- 아래 build.gradle은 개인 프로젝트 환경으로 다를 수 있으며 자세한 설정은 아래 링크들을 참고
- https://www.baeldung.com/junit-5
- https://www.baeldung.com/junit-5-gradle
- https://www.baeldung.com/spring-boot-testing
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());
}
}