ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Mock 객체 만들기
    백기선(인프런 강의)/더 자바, 애플리케이션을 테스트하는 다양한 방법 2020. 5. 26. 09:01
    반응형

    https://github.com/keesun/inflearn-the-java-test

     

    위에 github 저장소에 들어가 history를 아래 순서와 같이 history가 쌓여 있다.

     

     

     

    이중 Prepare Mockito example를 import를 받고 실행하면 아래와같이 import가 된다.(test부분은 삭제했음)

     

     

     

    방법1. 아무것도 사용안할 시 테스트하는 방법

    StudyService class를 테스트하기위해서는 아래와 같이 구현하여 테스트를 해야 한다.

     

    하지만 아래와 같이 구현체를 직접 구현해서 사용해야하는 번거로움이 있다.

    import static org.junit.Assert.assertNotNull;
    
    import java.util.List;
    import java.util.Optional;
    
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.extension.ExtendWith;
    import org.mockito.junit.jupiter.MockitoExtension;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.domain.Example;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.Pageable;
    import org.springframework.data.domain.Sort;
    import org.springframework.test.context.ActiveProfiles;
    import org.testcontainers.junit.jupiter.Testcontainers;
    
    import lombok.extern.slf4j.Slf4j;
    import me.whiteship.inflearnthejavatest.domain.Member;
    import me.whiteship.inflearnthejavatest.domain.Study;
    import me.whiteship.inflearnthejavatest.member.MemberService;
    
    class StudyServiceTest {
    	
    	@Test
    	void createStudyService() {
    		MemberService memberService = new MemberService() {
    			@Override
    			public Optional<Member> findById(Long memberId) {
    				// TODO Auto-generated method stub
    				return null;
    			}
    
    			@Override
    			public void validate(Long memberId) {
    				// TODO Auto-generated method stub
    				
    			}
    
    			@Override
    			public void notify(Study newstudy) {
    				// TODO Auto-generated method stub
    				
    			}
    
    			@Override
    			public void notify(Member member) {
    				// TODO Auto-generated method stub
    				
    			}
    		};
    		StudyRepository studyRepository = new StudyRepository() {
    			
    			@Override
    			public <S extends Study> Optional<S> findOne(Example<S> example) {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public <S extends Study> Page<S> findAll(Example<S> example, Pageable pageable) {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public <S extends Study> boolean exists(Example<S> example) {
    				// TODO Auto-generated method stub
    				return false;
    			}
    			
    			@Override
    			public <S extends Study> long count(Example<S> example) {
    				// TODO Auto-generated method stub
    				return 0;
    			}
    			
    			@Override
    			public <S extends Study> S save(S entity) {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public Optional<Study> findById(Long id) {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public boolean existsById(Long id) {
    				// TODO Auto-generated method stub
    				return false;
    			}
    			
    			@Override
    			public void deleteById(Long id) {
    				// TODO Auto-generated method stub
    				
    			}
    			
    			@Override
    			public void deleteAll(Iterable<? extends Study> entities) {
    				// TODO Auto-generated method stub
    				
    			}
    			
    			@Override
    			public void deleteAll() {
    				// TODO Auto-generated method stub
    				
    			}
    			
    			@Override
    			public void delete(Study entity) {
    				// TODO Auto-generated method stub
    				
    			}
    			
    			@Override
    			public long count() {
    				// TODO Auto-generated method stub
    				return 0;
    			}
    			
    			@Override
    			public Page<Study> findAll(Pageable pageable) {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public <S extends Study> S saveAndFlush(S entity) {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public <S extends Study> List<S> saveAll(Iterable<S> entities) {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public Study getOne(Long id) {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public void flush() {
    				// TODO Auto-generated method stub
    				
    			}
    			
    			@Override
    			public List<Study> findAllById(Iterable<Long> ids) {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public <S extends Study> List<S> findAll(Example<S> example, Sort sort) {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public <S extends Study> List<S> findAll(Example<S> example) {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public List<Study> findAll(Sort sort) {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public List<Study> findAll() {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public void deleteInBatch(Iterable<Study> entities) {
    				// TODO Auto-generated method stub
    				
    			}
    			
    			@Override
    			public void deleteAllInBatch() {
    				// TODO Auto-generated method stub
    				
    			}
    		};
    		
    		StudyService studyService = new StudyService(memberService, studyRepository);
    		
    		assertNotNull(studyService);
    	}
    }

     

     

    방법2. Mockito.mock()사용할 시

    import static org.junit.Assert.assertNotNull;
    import static org.mockito.Mockito.mock;
    
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.extension.ExtendWith;
    import org.mockito.Mockito;
    import org.mockito.junit.jupiter.MockitoExtension;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.ActiveProfiles;
    import org.testcontainers.junit.jupiter.Testcontainers;
    
    import lombok.extern.slf4j.Slf4j;
    import me.whiteship.inflearnthejavatest.member.MemberService;
    
    class StudyServiceTest {
    
    	@Test
    	void createStudyService() {
    		MemberService memberService = mock(MemberService.class);
    
    		StudyRepository studyRepository = Mockito.mock(StudyRepository.class);
    
    		StudyService studyService = new StudyService(memberService, studyRepository);
    
    		assertNotNull(studyService);
    	}
    }

     

     

    @Mock 애노테이션으로 만드는 방법1 - mock객체를 여러 테스트를 위해서 사용할 때

    import static org.junit.Assert.assertNotNull;
    
    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.member.MemberService;
    
    @ExtendWith(MockitoExtension.class)
    class StudyServiceTest {
    	
    	@Mock
    	MemberService memberService;
    	
    	@Mock
    	StudyRepository studyRepository;
    
    	@Test
    	void createStudyService() {
    		StudyService studyService = new StudyService(memberService, studyRepository);
    
    		assertNotNull(studyService);
    	}
    }

     

    @Mock 애노테이션으로 만드는 방법2 - mock객체 를 하나의 테스트에만 사용할 경우

    import static org.junit.Assert.assertNotNull;
    
    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.member.MemberService;
    
    @ExtendWith(MockitoExtension.class)
    class StudyServiceTest {
        
        @Test
        void createStudyService(@Mock MemberService memberService,
                                @Mock StudyRepository studyRepository) {
            StudyService studyService = new StudyService(memberService, studyRepository);
            assertNotNull(studyService);
        }
    }

     

     

    반응형
Designed by Tistory.