Open KIM-KYOUNG-OH opened 1 year ago
@ExtendWith(MockitoExtension.class) class RecruitServiceTest { @InjectMocks private RecruitService recruitService; @Spy private List<CompanyCondition> companyConditions = new ArrayList<>(); @Mock private Kakao kakao; @Mock private Naver naver; @Mock private EntireCompany entireCompany; @Before public void setUp() { MockitoAnnotations.initMocks(this); companyConditions.add(entireCompany); companyConditions.add(kakao); companyConditions.add(naver); } @Mock RecruitMapper recruitMapper; @Test public void getRecruitsByCompanySuccessTest() { //given Recruit recruit1 = new Recruit.Builder("testTitle1", "testCompany1", "testLink1") .addDateTime(LocalDateTime.now().minusHours(1)) .build(); Recruit recruit2 = new Recruit.Builder("testTitle2", "testCompany2", "testLink2") .addDateTime(LocalDateTime.now().minusHours(1)) .build(); Recruit recruit3 = new Recruit.Builder("testTitle3", "testCompany3", "testLink3") .addDateTime(LocalDateTime.now().minusDays(7)) .build(); when(recruitMapper.findByCompany(any(String.class))).thenReturn(Arrays.asList(recruit1, recruit2, recruit3)); when(entireCompany.isSatisfiedBy(any(CompanyType.class))).thenReturn(false); when(kakao.isSatisfiedBy(any(CompanyType.class))).thenReturn(true); when(naver.isSatisfiedBy(any(CompanyType.class))).thenReturn(false); when(kakao.getRecruitsByCondition()).thenReturn(Arrays.asList(recruit1, recruit2, recruit3)); //when CompanyType testCompanyType = CompanyType.KAKAO; List<Recruit> find = recruitService.getRecruitsByCompany(testCompanyType); //then verify(entireCompany.isSatisfiedBy(testCompanyType), times(1)); verify(naver.isSatisfiedBy(testCompanyType), times(1)); verify(kakao.isSatisfiedBy(testCompanyType), times(1)); verify(entireCompany.getRecruitsByCondition(), times(0)); verify(naver.getRecruitsByCondition(), times(0)); verify(kakao.getRecruitsByCondition(), times(1)); assertThat(find.size()).isEqualTo(3); assertThat(find).contains(recruit1, recruit2, recruit3); } }
https://stackoverflow.com/questions/33302340/how-to-mock-a-autowired-list-of-spring-beans