단위 테스트를 위해 junit에 대해 공부했다.
JUnit4.x :: @Runwith(SpringRunner.class)
JUnit5.x :: @SpringBootTest
--------------------------------------------------------------
DomainTest
--------------------------------------------------------------
vo entity dto --------------------------------------------------- 단위
--------------------------------------------------------------
Repository Test : CURD DataSource
--------------------------------------------------------------
@DataJpaTest --------------------------------------------------- 단위
- 기본적으로 @Transactional 포함
- 기본적으로 내부 DB 사용해 테스트 (H2)
- 레포지토리 : @ExtendWith(SpringExtension.class) : JUnit5 부터 제공, 가짜객체를 사용하겠다
- @Autowired private BoardRepository
- 서비스 : @ExtendWith(MockitoExtension.class)
테스트주체: @InjectMocks private BoardServiceImpl boardService;
테스트협력자@Mock private BoardRepository boardRepository;
- 컨트롤러 : @ExtendWith(MockitoExtension.class)
테스트주체: @InjectMocks private BoardController boardService;
테스트협력자@Mock private BoardRepository boardRepository;
private BoardService baordService;
--------------------------------------------------------------
Service Test
--------------------------------------------------------------
@SpringBootTest --------------------------------------------------- 통합
@AutoConfigureMockMvc
@Autowired private BoardServiceImpl (진짜)
@MockBean private BoardRepository (가짜)
@ExtendWith(MockitoExtension.class)
@InjectMocks private BoardServiceImpl boardService;
@Mock private BoardRepository boardRepository;
--------------------------------------------------------------
Controller Test
--------------------------------------------------------------
@WebMvcTest(BoardController.class) : ctl만 테스트 --------------- 통합
public class BoardControllerTest {
@Autowired private MockMvc mockMvc;
@MockBean private BoardService boardService;
@SpringBootTest
public class BoardControllerTest { ---------------------- 통합
@Autowired private BoardSrevice boardService;
--------------------------------------------------------------
@SpringBootTest (ctl - svc - repo)
--------------------------------------------------------------
: @Transactional 관리를 직접 해야한다.
::: 테스트 슬라이드
@WebMvcTest : (ctl) --> mvc 테스트
@DataJpaTest : CRUD , DataSource (스캔제외 : @Ctl, @Svc, @Com)
@JsonTest : JSON 직렬화/역직렬화 (스캔제외 : @Ctl, @Svc, @Rep, @Com)
@RestClientTest : HTTP 클라이언트 값/동작 테스트 (스캔제외 : @Ctl, @Svc, @Rep, @Com)
@Mock(단위)
- Mockito를 사용하여 모의 객체(Mock)를 생성
- 단위 테스트에서 주로 사용
- 스프링 컨텍스트와 무관하게 동작
- 빠르고 간단하게 모의 객체 생성 (스프링빈 주입 테스트 불가)
@InjectMocks(단위)
모의 객체를 주입하여 테스트할 클래스의 인스턴스 생성
@Mock으로 생성된 모의 객체 주입(스프링빈 주입 테스트 불가)
@WebMvcTest (단위) -- @AutoConfigureMockMvc 차이(통합)
- 특정 컨트롤러만 테스트하며, 빠르고 간단한 단위 테스트에 적합
- (컨트롤러, 인터셉터, 컨트롤러 어드바이스 등)만 로드
- 주로 단위 테스트에 사용
@AutoConfigureMockMvc(통합)
- @SpringBootTest와 함께 사용되어 전체 애플리케이션 컨텍스트를 로드
- MockMvc 인스턴스 자동 구성 (컨트롤러, 서비스, 레포지토리 등 모든 빈을 로드)
- 주로 통합 테스트에 사용
- @Autowired를 사용하여 테스트 클래스에서 MockMvc 인스턴스 주입
- 실제 서버를 시작하지 않고도 다양한 웹 요청과 응답 테스트
@MockBean(통합)
- @AutoConfigureMockMvc 와 함께 사용되어 전체 애플리케이션 컨텍스트를 로드
- 스프링 컨텍스트 내에서 실제 스프링 빈을 모의 객체 빈으로 등록
- 주로 통합 테스트에서 사용
- 스프링의 의존성 주입 테스트
- 스프링 컨텍스트를 로드 : 느린속도, 많은 리소스, 복잡도 증가
------------------------------------------------------
Assert : 테스트 검증
------------------------------------------------------
\org.junit.Assert.assertEquals
org.junit.Assert.assertArrayEquals
org.junit.Assert.assertSame
org.junit.Assert.assertNotSame
org.junit.Assert.assertTrue
org.junit.Assert.assertFalse
org.junit.Assert.assertNull
org.junit.Assert.assertNotNull
org.junit.Assert.fail
**org.junit.Assert.assertThat
assertEquals(비교대상, 비교값)
assertSame(비교대상, 비교값)
assertThat(비교대상).isEqualsTo(비교값)
assertThat(비교대상).hasSizeGraterThanOrEqualsTo(비교값)
assertThat(비교대상).isPresent() .isNotPresent()
assertThat(비교대상).isNull() .isNotNull()
maven
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
gradle
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.10.2'
yaml
testImplementation
org.mybatis.spring.boot
mybatis-spring-boot-starter-test
3.0.3'
'KOSTA교육 > 수업' 카테고리의 다른 글
[76/100] 240731 (0) | 2024.07.31 |
---|---|
[75/100] 240730 (0) | 2024.07.30 |
[73/100] 240726 (0) | 2024.07.26 |
[72/100] 240725 (0) | 2024.07.25 |
[71/100] 240724 (0) | 2024.07.24 |