티스토리 뷰
Spring Test : Repository Integration Test with @RunWith, @DataJpaTest
Korean Eagle 2020. 7. 31. 23:201. 이 포스트는 JUnit 4 버전으로 작성되었다.
2. Integration Test를 위해서는 Application Context를 생성하여 한다.
2-1 이것을 위해 @RunWith라는 annotation으로 테스트를 수행하는 지정된 클래스를 실행하게 된다.
2-2 테스트를 위한 간단한 Context를 실행한다고 생각할 수 있고, 보통 JUnit 4에서는 SpringRunner.class가 지정된다.
2-3 SpringRunner.class는 이전에 SpringJUnit4ClassRunner의 이름만 바꾼거다.
3. JPA 테스트를 위하여 @DataJpaTest라는 것이 제공된다.
3-1 내부적으로는 auto-configuration이 모두 disable되고 JPA테스트와 관련된 부분만 활성화 된다.
3-2 @DataJpaTest가 지정된 클래스의 메소드는 모두 @Transactional이 설정되어 별도의 트랜젝션으로 수행된다.
3-2-1 각 테스트를 수행 후에는 Roll back된다.
3-3 기본적으로 외부 설정 파일이나 자동설정기능에서 지정한 datasource는 무시되고 내장 데이터베이스를 사용한다.
3-4 다른 데이터베이스를 원하면 @AutoConfigureTestDatabase를 사용하여 지정할 수 있다.
3-5 JPA테스트 기능 뿐 아니라 모든 스프링의 기능을 사용하고 싶고 내장 데이터베이스를 사용하려면
3-5-1 SpringBootTest, @AutoConfigureTestDatabase 함께 사용하여 설정할 수 있다.
package pe.pilseong.recipe.repository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import pe.pilseong.recipe.domain.UnitOfMeasure;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Optional;
import org.junit.Before;
@RunWith(SpringRunner.class)
@DataJpaTest
public class UnitOfMeasureRepositoryIT {
@Autowired
UnitOfMeasureRepository unitOfMeasureRepository;
@Before
public void setup(){
}
@Test
@DirtiesContext
public void findbyDescription() {
Optional<UnitOfMeasure> uom = unitOfMeasureRepository.findByDescription("Teaspoon");
assertEquals("Teaspoon", uom.get().getDescription());
}
@Test
@DirtiesContext
public void findbyDescriptionCup() {
Optional<UnitOfMeasure> uom = unitOfMeasureRepository.findByDescription("Cup");
assertEquals("Cup", uom.get().getDescription());
}
@Test
public void test() {
}
}
'Spring > Spring Test' 카테고리의 다른 글
Spring Test : Spring Boot 2.1 이전 버전에서의 Junit 5 설정하기 (0) | 2020.08.02 |
---|---|
Spring Test : Maven Integration Test 설정하기 (0) | 2020.08.01 |
Spring Test : MockMvc 사용하여 MVC Controller 테스트 하기 (0) | 2020.07.31 |
Spring Test : Argument 캡처하기 (0) | 2020.07.31 |
Spring Test : Controller 레이어 클래스 테스트 (0) | 2020.07.31 |
- Total
- Today
- Yesterday
- 도커 개발환경 참고
- AWS ARN 구조
- Immuability에 관한 설명
- 자바스크립트 멀티 비동기 함수 호출 참고
- WSDL 참고
- SOAP 컨슈머 참고
- MySql dump 사용법
- AWS Lambda with Addon
- NFC 드라이버 linux 설치
- electron IPC
- mifare classic 강의
- go module 관련 상세한 정보
- C 메모리 찍어보기
- C++ Addon 마이그레이션
- JAX WS Header 관련 stackoverflow
- SOAP Custom Header 설정 참고
- SOAP Custom Header
- SOAP BindingProvider
- dispatcher 사용하여 설정
- vagrant kvm으로 사용하기
- git fork, pull request to the …
- vagrant libvirt bridge network
- python, js의 async, await의 차이
- go JSON struct 생성
- Netflix Kinesis 활용 분석
- docker credential problem
- private subnet에서 outbound IP 확…
- 안드로이드 coroutine
- kotlin with, apply, also 등
- 안드로이드 초기로딩이 안되는 경우
- navigation 데이터 보내기
- 레이스 컨디션 navController
- raylib
- 하이버네이트
- 상속
- Angular
- 로그인
- 외부파일
- Security
- mapping
- login
- one-to-one
- XML
- spring boot
- Spring
- 설정하기
- jsp
- Rest
- one-to-many
- crud
- Spring Security
- RestTemplate
- hibernate
- Validation
- 스프링
- MYSQL
- WebMvc
- 매핑
- form
- 스프링부트
- 자바
- 설정
- Many-To-Many