Gradle 의 테스트 사이클에서 원하는 테스트 클래스를 찾는 키워드는 --tests 이다.
원하는 타겟클래스는 이러하다.
@DataMongoTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class PersistenceTests {
@Autowired
private ProductRepository productRepository;
private ProductEntity productEntity;
@BeforeEach
public void setUpDb() {
System.out.println("Before");
productRepository.deleteAll();;
ProductEntity entity = new ProductEntity(1,"n",1);
productEntity = productRepository.save(entity);
assertEqualsProduct(entity,productEntity);
}
@Test
public void create() {
ProductEntity newEntity = new ProductEntity(2,"n",2);
productRepository.save(newEntity);
// 고유 값은 String 이며 API 에 공개되지 않는다!
ProductEntity foundEntity = productRepository.findById(newEntity.getId()).get();
assertEqualsProduct(newEntity,foundEntity);
assertEquals(2,productRepository.count());
}
@Test
public void update() {
productEntity.setName("n2");
productRepository.save(productEntity);
ProductEntity foundEntity = productRepository.findById(productEntity.getId()).get();
assertEquals(1, (long)foundEntity.getVersion());
assertEquals("n2",foundEntity.getName());
}
@Test
public void delete() {
//@Before 에서 온 녀석
productRepository.delete(productEntity);
assertFalse(productRepository.existsById(productEntity.getId()));
}
@Test
public void getByProductId() {
Optional<ProductEntity> entity = productRepository.findByProductId(productEntity.getProductId());
assertTrue(entity.isPresent());
assertEqualsProduct(productEntity,entity.get());
}
@Test
public void duplicateError() {
ProductEntity entity = new ProductEntity(productEntity.getProductId(),"n",1);
assertThrows(DuplicateKeyException.class, () -> productRepository.save(entity));
}
@Test
public void optimisticLockError() {
ProductEntity entity1 = productRepository.findById(productEntity.getId()).get();
ProductEntity entity2 = productRepository.findById(productEntity.getId()).get();
// Version 증가
entity1.setName("n1");
productRepository.save(entity1);
try {
entity2.setName("n2");
productRepository.save(entity2);
fail("Expected an OptimisticLockingFailureException");
}catch (OptimisticLockingFailureException e) {}
ProductEntity updateEntity = productRepository.findById(productEntity.getId()).get();
assertEquals(1,(int)updateEntity.getVersion());
assertEquals("n1",updateEntity.getName());
}
@Test
public void paging() {
productRepository.deleteAll();
List<ProductEntity> newProducts = rangeClosed(1001,1010)
.mapToObj(value -> new ProductEntity(value,"name "+value,value))
.collect(Collectors.toList());
productRepository.saveAll(newProducts);
Pageable nextPage = PageRequest.of(0,4, Sort.Direction.ASC,"productId");
nextPage = testNextPage(nextPage,"[1001, 1002, 1003, 1004]",true);
nextPage = testNextPage(nextPage,"[1005, 1006, 1007, 1008]",true);
nextPage = testNextPage(nextPage,"[1009, 1010]",false);
}
private Pageable testNextPage(Pageable nextPage, String expectedProductId, boolean expectedNextPage) {
Page<ProductEntity> productPage = productRepository.findAll(nextPage);
assertEquals(expectedProductId,productPage.getContent().stream().map(p -> p.getProductId()).collect(Collectors.toList()).toString());
assertEquals(expectedNextPage,productPage.hasNext());
return productPage.nextPageable();
}
private void assertEqualsProduct(ProductEntity expectedEntity, ProductEntity actualEntity) {
assertEquals(expectedEntity.getId(), actualEntity.getId());
assertEquals(expectedEntity.getVersion(), actualEntity.getVersion());
assertEquals(expectedEntity.getProductId(), actualEntity.getProductId());
assertEquals(expectedEntity.getName(), actualEntity.getName());
assertEquals(expectedEntity.getWeight(), actualEntity.getWeight());
}
}
이 클래스를 타겟으로 Test 사이클을 돌리려면 :test 사이클을 주고 --tests 키워드로 원하는 테스트를 뽑아온다.
# :test 사이클 시작 , --tests [TargetClass]
gradlew microservices:product-service:test --tests PersistenceTests
'Etc' 카테고리의 다른 글
WebFlux.fn 에서 전역범위 Validator를 만들어보자. (0) | 2021.04.20 |
---|---|
WebFlux 사양의 슬라이스 테스트 (0) | 2021.04.15 |
Jq ( JSON Parser ) 설치 (0) | 2021.01.02 |
수강하며 드는 개인적 생각을 모음 (0) | 2020.09.18 |