ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Mock객체 stubbing연습문제
    백기선(인프런 강의)/더 자바, 애플리케이션을 테스트하는 다양한 방법 2020. 5. 28. 23:25
    반응형

     

    import static org.junit.jupiter.api.Assertions.assertEquals;
    import static org.junit.jupiter.api.Assertions.assertNotNull;
    import static org.junit.jupiter.api.Assertions.assertThrows;
    import static org.mockito.ArgumentMatchers.any;
    import static org.mockito.Mockito.when;
    
    import java.util.Optional;
    
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.extension.ExtendWith;
    import org.mockito.Mock;
    import org.mockito.junit.jupiter.MockitoExtension;
    
    import me.whiteship.inflearnthejavatest.domain.Member;
    import me.whiteship.inflearnthejavatest.domain.Study;
    import me.whiteship.inflearnthejavatest.member.MemberService;
    
    @ExtendWith(MockitoExtension.class)
    class StudyServiceTest {
    
    	@Test
    	void createNewStudy(@Mock MemberService memberService, @Mock StudyRepository studyRepository) {
    		StudyService studyService = new StudyService(memberService, studyRepository);
    		assertNotNull(studyService);
    
    		Member member = new Member();
    		member.setId(1L);
    		member.setEmail("rlawls1991@mail.com");
    		
    		//1. memberSErvice객체에 findByid 메소드를 1L값으로 호출하면 Optional.of(member) 객체를 리턴하도록 Stubbing
    		when(memberService.findById(1L)).thenReturn( Optional.of(member));
    		
    		//2. studyRepository 객체에 save 메소드를 study 객체로 호출하면 study 객체 그대로 리턴하도록 study;
    		Study study = new Study(10, "test");
    		when(studyRepository.save(study)).thenReturn(study);
    		
    
    		studyService.createNewStudy(1L, study);
    		assertNotNull(study.getOwner());
    		assertEquals(member, study.getOwner());
    	}
    
    }
    반응형

    '백기선(인프런 강의) > 더 자바, 애플리케이션을 테스트하는 다양한 방법' 카테고리의 다른 글

    BDD 스타일 Mockito API  (0) 2020.06.02
    Mock 객체 확인  (0) 2020.05.29
    Mock 객체 Stubbing  (0) 2020.05.27
    Mock 객체 만들기  (0) 2020.05.26
    Mockito  (0) 2020.05.24
Designed by Tistory.