Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

#36 junit 이용한 테스트 코드 작성 #37

Open
wants to merge 18 commits into
base: feature/30
Choose a base branch
from

Conversation

junshock5
Copy link
Collaborator

junit 이용한 테스트 코드 작성

  • Mockito Framework를 활용하여 고립된 테스트 코드를 작성
  • Jenkins CI를 적용하여 테스트 자동화
  • 협업하는 동료의 소스코드에 서로 테스트코드를 작성하여 서로의 소스코드를 알 수 있도록 하고 있습니다.

- spring-boot-starter 안에 junit 및 mockito framework가 포함됨으로 기존 필요없는 의존성 삭제
- README 파일에 테스트 내용 추가
- UserServcieImplTest api 메서드 작성
@junshock5 junshock5 self-assigned this Sep 21, 2020
@junshock5 junshock5 changed the base branch from develop to feature/30 September 21, 2020 15:38
- ProductServiceImplTest api 메서드 작성
- CategoryServiceImplTest api 메서드 작성
- ProductServiceImplTest api 메서드에 ProductServiceImpl의 api 메서드 테스트 추가
- productDao에서 캐시 삭제부분 예외처리 대신 에러 로그로 변경
- ProductSearchServiceImplTest의 api 메서드 테스트 추가
@junshock5 junshock5 changed the title junit 이용한 테스트 코드 작성 #36 junit 이용한 테스트 코드 작성 Sep 21, 2020
@junshock5 junshock5 added the review wanted 리뷰 부탁드립니다. label Sep 21, 2020
productId = productDao.selectProductsIndex(productId);
if (productDao.deleteByProductIdAndIndex(ProductDTO.DEFAULT_PRODUCT_SEARCH_CACHE_KEY, productId) == false) {
throw new RuntimeException("물품 레디스 리스트에서 삭제 실패!");
log.error("물품 레디스 리스트에서 삭제 실패! {}", productId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

에러만 찍고 예외를 던지지 않는 이유가 있을까요? 이 메소드를 호출하는 입장에서는 정상 처리된 것으로 보일 수도 있을 것 같네요~

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

단순하게 예외를 던지면 테스트가 실패하여 변경하였습니다.

Copy link
Collaborator Author

@junshock5 junshock5 Sep 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

레디스에 있는 캐싱된 데이터 까지 삭제하려면 테스트 코드에서
@Mock
private RedisTemplate<String, Object> redisTemplate;
캐싱 데이터를 사전에 넣어야 할까요??

CategoryDTO categoryDTO = generateCategory();
categoryDTO.setName("testName2");
categoryMapper.updateCategory(categoryDTO);
assertTrue(categoryDTO.getName().equals("testName2"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. assertEquals를 해주시면 좋을 것 같네요~
  2. categoryDTO.setName("testName2"); 때문에 언제나 이것은 true가 리턴될 것 같은데요. 이건 올바른 테스트일까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setName 후에
categoryMapper.updateCategory(categoryDTO); 변경후
실제 Name필드가 정상 바뀌었는지 확인하기 위한 테스트인데
일부분만 테스트 된다고 생각은 됩니다.

그러면 assertEquals에 전체 필드를 확인해야 할까요??

void findAllProductsByCacheId() {
MockitoAnnotations.initMocks(this); // mock all the field having @Mock annotation
UserDTO userDTO = generateUser();
productDao.findAllProductsByCacheId(userDTO.getId());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findAllProductsByCacheId 내부에서 일어나는 로직 검사라던지 리턴값이라던지 그런 스펙에 대한 검사가 없네요~

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mock
private RedisTemplate<String, Object> redisTemplate;

@Mock
private ValueOperations valueOperations;

선언 후에
redisTemplate.opsForList().rightPush(ProductDTO.DEFAULT_PRODUCT_SEARCH_CACHE_KEY, productDTO);
여기서 redisTemplate이 mock객체로 null은 아니지만 List,push 할때 null객체로 에러가 납니다.
접근을 어떻게 해야 할까요??

- deleteProduct 메서드에 실패시 예외로 변경
- 테스트시 Mock 어노테이션으로 redisTemplate에 데이터 추가하여 캐싱 처리 중..
- assertTrue -> assertEquals 메서드 변경

// Mock redisTemplate 테스트
//redisTemplate.opsForList().rightPush(ProductDTO.DEFAULT_PRODUCT_SEARCH_CACHE_KEY, productDTO);
//productService.deleteProduct(userDTO.getAccountId(), productDTO.getId());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 PR은 실제 코드에 합치려고 보내는것인데 이런 주석들이 실제 코드에 쌓이면 어떻게 될까요?

@f-lab-dev
Copy link
Member

여기서 보면 CI/CD가 잘못 적용된 것 같습니다~ 3개가 동시에 돌아갔네요. 여기는 CI만 적용하는게 맞습니다~
이 책을 한번 읽으시면 좋을 듯 하네요

https://book.naver.com/bookdb/book_detail.nhn?bid=14048938

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
review wanted 리뷰 부탁드립니다.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants