통합 테스트 (Integration Test)
- 두 개 이상의 모듈이 연결된 상태를 테스트할 수 있음.
- 모듈 간의 연결에서 발생하는 에러 검증 가능함.
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) // 서버의 PORT 를 랜덤으로 설정합니다.
@TestInstance(TestInstance.Lifecycle.PER_CLASS) // 테스트 인스턴스의 생성 단위를 클래스로 변경합니다.
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class ProductServiceIntegrationTest {
@Autowired
ProductService productService;
@Autowired
UserRepository userRepository;
User user;
ProductResponseDto createdProduct = null;
int updatedMyPrice = -1;
@Test
@Order(1)
@DisplayName("신규 관심상품 등록")
void test1() {
// given
String title = "Apple <b>에어팟</b> 2세대 유선충전 모델 (MV7N2KH/A)";
String imageUrl = "https://shopping-phinf.pstatic.net/main_1862208/18622086330.20200831140839.jpg";
String linkUrl = "https://search.shopping.naver.com/gate.nhn?id=18622086330";
int lPrice = 173900;
ProductRequestDto requestDto = new ProductRequestDto(
title,
imageUrl,
linkUrl,
lPrice
);
user = userRepository.findById(1L).orElse(null);
// when
ProductResponseDto product = productService.createProduct(requestDto, user);
// then
assertNotNull(product.getId());
assertEquals(title, product.getTitle());
assertEquals(imageUrl, product.getImage());
assertEquals(linkUrl, product.getLink());
assertEquals(lPrice, product.getLprice());
assertEquals(0, product.getMyprice());
createdProduct = product;
}
@Test
@Order(2)
@DisplayName("신규 등록된 관심상품의 희망 최저가 변경")
void test2() {
// given
Long productId = this.createdProduct.getId();
int myPrice = 173000;
ProductMypriceRequestDto requestDto = new ProductMypriceRequestDto();
requestDto.setMyprice(myPrice);
// when
ProductResponseDto product = productService.updateProduct(productId, requestDto);
// then
assertNotNull(product.getId());
assertEquals(this.createdProduct.getTitle(), product.getTitle());
assertEquals(this.createdProduct.getImage(), product.getImage());
assertEquals(this.createdProduct.getLink(), product.getLink());
assertEquals(this.createdProduct.getLprice(), product.getLprice());
assertEquals(myPrice, product.getMyprice());
this.updatedMyPrice = myPrice;
}
@Test
@Order(3)
@DisplayName("회원이 등록한 모든 관심상품 조회")
void test3() {
// given
// when
Page<ProductResponseDto> productList = productService.getProducts(user,
0, 10, "id", false);
// then
// 1. 전체 상품에서 테스트에 의해 생성된 상품 찾아오기 (상품의 id 로 찾음)
Long createdProductId = this.createdProduct.getId();
ProductResponseDto foundProduct = productList.stream()
.filter(product -> product.getId().equals(createdProductId))
.findFirst()
.orElse(null);
// 2. Order(1) 테스트에 의해 생성된 상품과 일치하는지 검증
assertNotNull(foundProduct);
assertEquals(this.createdProduct.getId(), foundProduct.getId());
assertEquals(this.createdProduct.getTitle(), foundProduct.getTitle());
assertEquals(this.createdProduct.getImage(), foundProduct.getImage());
assertEquals(this.createdProduct.getLink(), foundProduct.getLink());
assertEquals(this.createdProduct.getLprice(), foundProduct.getLprice());
// 3. Order(2) 테스트에 의해 myPrice 가격이 정상적으로 업데이트되었는지 검증
assertEquals(this.updatedMyPrice, foundProduct.getMyprice());
}
}
- Spring Boot를 이용한 통합 테스트
- 통합 테스트
- 여러 단위 테스트를 하나의 통합된 테스트로 수행함.
- 단위 테스트 시 Spring은 동작되지 않음.
- "@SpringBootTest"
- 스프링이 동작되도록 해주는 애너테이션.
- Spring IoC/DI 기능을 사용 가능
- Repository를 사용해 DB CRUD가 가능
- 스프링이 동작되도록 해주는 애너테이션.
- 통합 테스트
테스트 시작 전에 위에서 @Autowired 를 통해 의존시키고, 밑에서 테스트를 하는 동안 계속 쓸 변수들을 선언 및 초기화시켜줘서 계속 사용하게 할 수 있다.
'컴퓨터 프로그래밍 > Spring' 카테고리의 다른 글
[Spring] Controller Test Code 작성 (0) | 2024.09.14 |
---|---|
[Spring] AOP (0) | 2024.09.10 |
[Spring] Mockito (0) | 2024.09.09 |
[Spring] 단위 테스트 프레임워크 JUnit5, Given-When-Then 패턴 (0) | 2024.09.09 |
[Spring] 인터페이스의 default 메서드 (0) | 2024.09.01 |