Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
Tags
- Selection Sorting
- s
- JSON
- stream
- C programming
- Stack
- insertion sort
- Algorithm
- 메모리구조
- C 언어 코딩 도장
- Serialization
- buffer
- 이스케이프 문자
- coding test
- datastructure
- 윤성우의 열혈 자료구조
- 혼자 공부하는 C언어
- 알기쉬운 알고리즘
- 이것이 자바다
- Graph
- 윤성우 열혈자료구조
- list 컬렉션
- R
Archives
- Today
- Total
Engineering Note
[Spring Boot] @DataJpaTest 본문
오늘은 JPA를 테스트하는 어노테이션, @DataJpaTest에 대해 정리하려고 한다.
@DataJpaTest
- JPA Reposiotry와 Entity에 대해서 테스트를 해주는 어노테이션이다.
- 이 어노테이션을 사용하면 자동 환경구성을 해주고, 기본적으로 Embbedded in-memory Database를 사용한다. @AutoConfigureTestDatabase를 사용해서 실제 DB 등으로 오버라이딩해서 사용할 수 있다.
- Junit4를 사용하면 JPA만 테스트하더라도 @RunWith(SpringRunner.class) 를 같이 사용해야 한다.
이 어노테이션을 사용하면 실제 DB를 사용하지 않고 정말 비즈니스 로직에 대해 점검 할 수 있다.
세팅 방법
build.gradle
testImplementation 'com.h2database:h2'
package com.security.auth.repository;
import com.security.auth.entity.User;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.ActiveProfiles;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest
class UserRepositoryTest {
@Autowired
private UserRepository userRepository;
@Test
@DisplayName("이메일로 사용자 조회")
void findByEmail_shouldReturnUser() {
// given
User user = new User();
user.setEmail("test10@example.com");
user.setPassword("encodedPassword");
user.setNickname("testUser");
userRepository.save(user);
// when
Optional<User> result = userRepository.findByEmail("test@example.com");
// then
assertThat(result).isPresent();
assertThat(result.get().getEmail()).isEqualTo("test@example.com");
}
}
@DataJpaTest는 기본적으로, h2데이터베이스를 사용하기 때문에, h2 데이터베이스에 대한 의존성을 추가하지 않으면 오류가 발생한다.
'Server > Spring' 카테고리의 다른 글
| [Spring] @RequestParam (0) | 2025.09.15 |
|---|---|
| [Spring] @PathVariable, @RequestParam (0) | 2025.08.24 |
| [Spring] 스프링 부트 핵심 구성 요소 (0) | 2025.07.05 |
| [Spring] @SpringBootApplication 어노테이션 (0) | 2025.07.04 |
| [Spring] 의존성 주입(DI)과 스프링 빈 (0) | 2025.06.21 |
Comments
