티스토리 뷰

Test/Mockito

[Mockito] 3. Mock 객체 확인

mandykr 2021. 12. 10. 12:26

StudyService 의 createNewStudy() 메소드를 통해 테스트

public class StudyService {

    private final MemberService memberService;

    private final StudyRepository repository;

    public StudyService(MemberService memberService, StudyRepository repository) {
        assert memberService != null;
        assert repository != null;
        this.memberService = memberService;
        this.repository = repository;
    }

    public Study createNewStudy(Long memberId, Study study) {
        Optional<Member> member = memberService.findById(memberId);
        study.setOwnerId(member.orElseThrow(() -> new IllegalArgumentException("Member doesn't exist for id: '" + memberId + "'")).getId());
        Study newstudy = repository.save(study);
        memberService.notify(newstudy);
        memberService.notify(member.get());
        return newstudy;
    }

    public Study openStudy(Study study) {
        study.open();
        Study openedStudy = repository.save(study);
        memberService.notify(openedStudy);
        return openedStudy;
    }

}

 

1. 메소드가 특정 매개변수로 몇번 호출 되었는지 확인

   - verify(memberService, times(1)).notify(study);

   - notify 메소드가 study를 매개변수로 1번 호출 되었는지 확인

@ExtendWith(MockitoExtension.class)
class StudyServiceTest {
    @Mock
    MemberService memberService;

    @Mock
    StudyRepository studyRepository;

    @Test
    void createStudy() {
        Member member = new Member();
        member.setId(1L);
        member.setEmail("mandy@gmail.com");
        when(memberService.findById(1L)).thenReturn(Optional.of(member));

        Study study = new Study(10, "java");
        when(studyRepository.save(study)).thenReturn(study);

        StudyService studyService = new StudyService(memberService, studyRepository);
        studyService.createNewStudy(1L, study);

        verify(memberService, times(1)).notify(study);
    }
}

 

2. 메소드가 전혀 호출되지 않았는지 확인

   - verify(memberService, never()).validate(any());

   - validate 메소드가 어떤 매개변수로도 전혀 호출되지 않았는지 확인

@ExtendWith(MockitoExtension.class)
class StudyServiceTest {
    @Mock
    MemberService memberService;

    @Mock
    StudyRepository studyRepository;

    @Test
    void createStudy() {
        Member member = new Member();
        member.setId(1L);
        member.setEmail("mandy@gmail.com");
        when(memberService.findById(1L)).thenReturn(Optional.of(member));

        Study study = new Study(10, "java");
        when(studyRepository.save(study)).thenReturn(study);

        StudyService studyService = new StudyService(memberService, studyRepository);
        studyService.createNewStudy(1L, study);

        verify(memberService, never()).validate(any());
    }
}

 

3. 어떤 순서대로 호출했는지 확인

   - notify(study) > notify(mmeber) 순서로 호출했는지 확인

@Test
void createStudy() {
    Member member = new Member();
    member.setId(1L);
    member.setEmail("mandy@gmail.com");
    when(memberService.findById(1L)).thenReturn(Optional.of(member));

    Study study = new Study(10, "java");
    when(studyRepository.save(study)).thenReturn(study);

    StudyService studyService = new StudyService(memberService, studyRepository);
    studyService.createNewStudy(1L, study);

    InOrder inOrder = inOrder(memberService);
    inOrder.verify(memberService).notify(study);
    inOrder.verify(memberService).notify(member);
}

 

4. 특정 시점 이후에 아무 일도 벌어지지 않았는지 확인

   - verifyNoMoreInteractions(memberService);

   - notify() 메소드를 호출한 이후에 추가로 mock을 사용하지 않는지 확인

@Test
void createStudy() {
    Member member = new Member();
    member.setId(1L);
    member.setEmail("mandy@gmail.com");
    when(memberService.findById(1L)).thenReturn(Optional.of(member));

    Study study = new Study(10, "java");
    when(studyRepository.save(study)).thenReturn(study);

    StudyService studyService = new StudyService(memberService, studyRepository);
    studyService.createNewStudy(1L, study);

    verify(memberService, times(1)).notify(study);
    verifyNoMoreInteractions(memberService);
}

 

>> 실행 결과

memberservice.createNewStudy 32번 라인에서

memberService.notify(member.get()); 를 호출하기 때문에 테스트 실패

 

5. 특정 시간 이내에 호출됐는지 확인

   - Finding redundant invocations 참고

   - JUnit 5의 assertTimeout 을 사용할 수 있음

 

 

 

 

 

출처

https://www.inflearn.com/course/the-java-application-test 더 자바, 애플리케이션을 테스트하는 다양한 방법(백기선)

728x90

'Test > Mockito' 카테고리의 다른 글

[Mockito] 4. BDD Mockito 스타일 API  (0) 2021.12.10
[Mockito] 2. Mock 객체를 만들어 테스트하기  (0) 2021.12.09
[Mockito] 1. Mockito 시작하기  (0) 2021.12.09