KIM-KYOUNG-OH / ChatBot

금일 업데이트된 채용 공고 및 뉴스 기사를 제공하는 카카오톡 챗봇입니다.
0 stars 0 forks source link

테스트 오작동 이슈 #5

Open KIM-KYOUNG-OH opened 1 year ago

KIM-KYOUNG-OH commented 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);
    }
}
KIM-KYOUNG-OH commented 1 year ago

https://stackoverflow.com/questions/33302340/how-to-mock-a-autowired-list-of-spring-beans