Part 1. Spring Framework
Ch 05. 예외처리
02. Junit, Mockito 설명
- Junit
-- 자바의 unit test를 위한 Framework
package com.fastcampus.programmin.dmaker.service;
import com.fastcampus.programmin.dmaker.dto.CreateDeveloper;
import com.fastcampus.programmin.dmaker.dto.DeveloperDto;
import com.fastcampus.programmin.dmaker.type.DeveloperLevel;
import com.fastcampus.programmin.dmaker.type.DeveloperSkillType;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest // application을 test할 때도 실제 환경과 동일하게 실행한다.
class dMakerServiceTest_Junit {
@Autowired // dMakerService를 injection
private dMakerService dMakerService;
@Test
public void testSomething() {
dMakerService.createDeveloper(CreateDeveloper.Request.builder()
.developerLevel(DeveloperLevel.SENIOR)
.developerSkillType(DeveloperSkillType.FRONT_END)
.experienceYears(12)
.memberId("memberId")
.name("name")
.age(32)
.build());
List<DeveloperDto> allEmployedDevelopers = dMakerService.getAllEmployedDevelopers();
System.out.println("====================");
System.out.println(allEmployedDevelopers);
System.out.println("====================");
}
}
<dMakerServiceTest_Junit.java>
- Mockito
-- db에 데이터 없이, 격리성을 높이며 테스트하는 방법 : Mocking
-- Mockito는 Spring에 기본 내장되어 있어 쉽게 사용 가능
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.dto.DeveloperDto;
import com.fastcampus.programmin.dmaker.entity.Developer;
import com.fastcampus.programmin.dmaker.type.DeveloperLevel;
import com.fastcampus.programmin.dmaker.type.DeveloperSkillType;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
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.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
@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;
@Test
public void testSomething() {
given(developerRepository.findByMemberId(anyString()))
.willReturn(Optional.of(Developer.builder()
.developerLevel(SENIOR)
.developerSkillType(FRONT_END)
.experienceYears(12)
.statusCode(StatusCode.EMPLOYED)
.name("name")
.age(12)
.build()));
DeveloperDetailDto developerDetail = dMakerService.getAllDeveloperDetail("memberId");
assertEquals(SENIOR, developerDetail.getDeveloperLevel());
assertEquals(FRONT_END, developerDetail.getDeveloperSkillType());
assertEquals(12, developerDetail.getExperienceYears());
}
}
<dMakerServiceTest_Mockito.java>
https://bit.ly/37BpXic
'[패스트캠퍼스] Spring' 카테고리의 다른 글
패스트캠퍼스 챌린지 35일차 (0) | 2022.02.27 |
---|---|
패스트캠퍼스 챌린지 34일차 (0) | 2022.02.26 |
패스트캠퍼스 챌린지 32일차 (0) | 2022.02.24 |
패스트캠퍼스 챌린지 31일차 (0) | 2022.02.23 |
패스트캠퍼스 챌린지 30일차 (0) | 2022.02.22 |
댓글