ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Mockito 기반의 테스트
    [공부] 프로그래밍/Spring・Spring Boot (JAVA) 2024. 4. 24. 15:36

     

    Mockito 기반의 테스트

    가짜(mock) 객체를 지원하는 테스트 프레임워크이다. 테스트를 진행 시 여러 객체들간의 의존성이 존재하기 때문에 이를 해결하기 위해서 가짜 객체를 주입시켜 테스트를 진행한다.

    • @Mock : Mock 객체를 만들어 변환해주는 어노테이션
    • @Spy : Stub하지 않은 메서드들은 원본 메서드 그대로 사용하는 어노테이션
    • @InjectionMocks : @Mock 또는 @Spy로 생성된 가짜 객체를 자동으로 주입시켜 주는 어노테이션

     

    Mockito와 Junit의 결합
    • @ExpendWith(MockitoExtension.class) 를 사용하여 결합 가능

     

    주요 기능
    • ArgumentCaptor
    이를 사용하여 메소드 호출 시 전달된 매개변수를 캡처하고 검증할 수 있습니다.
    여기서 ArgumentCaptor<List>를 사용하면 메서드가 호출될 때 전달 된 List 인스턴스를 캡처할 수 있습니다.
    // ArgumentCaptor 생성
    ArgumentCaptor<List> passEntitiesCaptor = ArgumentCaptor.forClass(List.class);
    
    // 메소드 호출
    bulkPassRepository.findByStatusAndStartedAtGreaterThan(BulkPassStatus.READY, any());
    
    // 캡처된 객체 가져오기
    verify(bulkPassRepository).findByStatusAndStartedAtGreaterThan(eq(BulkPassStatus.READY), passEntitiesCaptor.capture());
    
    // 캡처된 객체에서 List 추출
    List capturedList = passEntitiesCaptor.getValue();
    
    // 추출된 List에서 원하는 검증 수행
    assertEquals(1, capturedList.size());

     

    참조 : https://velog.io/@yyong3519/Mockito

     

    '[공부] 프로그래밍 > Spring・Spring Boot (JAVA)' 카테고리의 다른 글

    Spring Batch 구현 - 5  (0) 2024.05.08
    Spring Batch 구현 - 4  (0) 2024.04.25
    Spring Batch 구현 - 3  (0) 2024.04.24
    Spring Batch 구현 - 2  (0) 2024.04.18
    Spring Batch 구현 - 1  (0) 2024.04.16
Designed by Tistory.