Part 1. Spring Framework
Ch 05. 예외처리
04. Service 테스트 작성
package com.fastcampus.programmin.dmaker.service;
import com.fastcampus.programmin.dmaker.Repository.DeveloperRepository;
import com.fastcampus.programmin.dmaker.Repository.RetiredDeveloperRepository;
import com.fastcampus.programmin.dmaker.code.StatusCode;
import com.fastcampus.programmin.dmaker.dto.CreateDeveloper;
import com.fastcampus.programmin.dmaker.dto.DeveloperDetailDto;
import com.fastcampus.programmin.dmaker.entity.Developer;
import com.fastcampus.programmin.dmaker.exception.DMakerErrorCode;
import com.fastcampus.programmin.dmaker.exception.DMakerException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Optional;
import static com.fastcampus.programmin.dmaker.type.DeveloperLevel.SENIOR;
import static com.fastcampus.programmin.dmaker.type.DeveloperSkillType.FRONT_END;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@ExtendWith(MockitoExtension.class)
class dMakerServiceTest_Mockito {
//db에 데이터 없이, 격리성을 높이며 테스트하는 방법 : Mocking
//Mockito는 Spring에 기본 내장되어 있어 쉽게 사용 가능
@Mock
private DeveloperRepository developerRepository;
@Mock
private RetiredDeveloperRepository retiredDeveloperRepository;
@InjectMocks //Mock = 가짜, 가짜를 dMakerService에 주입한다는 뜻
// 위쪽의 2개의 Mock을 자동으로 주입
private DMakerService dMakerService;
private final Developer defaultDeveloper = Developer.builder()
.developerLevel(SENIOR)
.developerSkillType(FRONT_END)
.experienceYears(12)
.statusCode(StatusCode.EMPLOYED)
.name("name")
.age(12)
.build();
private final CreateDeveloper.Request defaultCreateRequest =
CreateDeveloper.Request.builder()
.developerLevel(SENIOR)
.developerSkillType(FRONT_END)
.experienceYears(12)
.memberId("memberId")
.name("name")
.age(32)
.build();
@Test
public void testSomething() {
given(developerRepository.findByMemberId(anyString()))
.willReturn(Optional.of(defaultDeveloper));
DeveloperDetailDto developerDetail = dMakerService.getAllDeveloperDetail("memberId");
assertEquals(SENIOR, developerDetail.getDeveloperLevel());
assertEquals(FRONT_END, developerDetail.getDeveloperSkillType());
assertEquals(12, developerDetail.getExperienceYears());
}
//22.2.28
@Test
void createDeveloperTest_success() {
//given
CreateDeveloper.Request request = CreateDeveloper.Request.builder()
.developerLevel(SENIOR)
.developerSkillType(FRONT_END)
.experienceYears(12)
.memberId("memberId")
.name("name")
.age(32)
.build();
given(developerRepository.findByMemberId(anyString()))
.willReturn(Optional.empty());
ArgumentCaptor<Developer> captor =
ArgumentCaptor.forClass(Developer.class);
//when
CreateDeveloper.Response developer = dMakerService.createDeveloper(defaultCreateRequest);
//then
verify(developerRepository, times(1))
.save(captor.capture());
Developer savedDeveloper = captor.getValue();
assertEquals(SENIOR, savedDeveloper.getDeveloperLevel());
assertEquals(FRONT_END, savedDeveloper.getDeveloperSkillType());
assertEquals(12, savedDeveloper.getExperienceYears());
//dMakerService.createDeveloper(request);
}
@Test
void createDeveloperTest_failed_with_duplicated() {
//given
given(developerRepository.findByMemberId(anyString()))
.willReturn(Optional.of(defaultDeveloper));
ArgumentCaptor<Developer> captor =
ArgumentCaptor.forClass(Developer.class);
//when
//then
//dMakerService.createDeveloper(request);
DMakerException dMakerException = assertThrows(DMakerException.class, () -> dMakerService.createDeveloper(defaultCreateRequest));
assertEquals(DMakerErrorCode.DUPLICATED_MEMBER_ID, dMakerException.getDMakerErrorCode());
}
}
<dMakerServiceTest_Mockito.java>
https://bit.ly/37BpXic
'[패스트캠퍼스] Spring' 카테고리의 다른 글
패스트캠퍼스 챌린지 36일차 (0) | 2022.02.28 |
---|---|
패스트캠퍼스 챌린지 34일차 (0) | 2022.02.26 |
패스트캠퍼스 챌린지 33일차 (0) | 2022.02.25 |
패스트캠퍼스 챌린지 32일차 (0) | 2022.02.24 |
패스트캠퍼스 챌린지 31일차 (0) | 2022.02.23 |
댓글