tonykang22 / study

0 stars 0 forks source link

[The JAVA, 테스트] 2부. Mockito #8

Open tonykang22 opened 2 years ago

tonykang22 commented 2 years ago

2부. Mokito

1. Mockito 소개



2. Mockito 시작하기


3. Mock 객체 만들기


예시

4. Mock 객체 Stubbing


예시




5. Mock 객체 Stubbing 연습문제

    Study study = new Study(10, "테스트");

    // TODO memberService 객체에 findById 메소드를 1L 값으로 호출하면 Optional.of(member) 객체를 리턴하도록 Stubbing
    // TODO studyRepository 객체에 save 메소드를 study 객체로 호출하면 study 객체 그대로 리턴하도록 Stubbing

    studyService.createNewStudy(1L, study);
    assertEquals(member, study.getOwner());



6. Mock 객체 확인


예시




7. Mockito BDD 스타일 API


예시

@Test
    void createNewStudy() {
        // Given
        StudyService studyService = new StudyService(memberService, studyRepository);
        assertNotNull(studyService);

        Member member = new Member();
        member.setId(1L);
        member.setEmail("keesun@email.com");

        Study study = new Study(10, "테스트");

        given(memberService.findById(1L)).willReturn(Optional.of(member));

        // When
        studyService.createNewStudy(1L, study);

        // Then
        assertEquals(1L, study.getOwnerId());
        then(memberService).should(times(1)).notify(study);
        then(memberService).shouldHaveNoMoreInteractions();
    }