|
| 1 | +package com.loopers.job.demo; |
| 2 | + |
| 3 | +import com.loopers.batch.job.demo.DemoJobConfig; |
| 4 | +import lombok.RequiredArgsConstructor; |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.DisplayName; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.springframework.batch.core.ExitStatus; |
| 9 | +import org.springframework.batch.core.Job; |
| 10 | +import org.springframework.batch.core.JobParametersBuilder; |
| 11 | +import org.springframework.batch.test.JobLauncherTestUtils; |
| 12 | +import org.springframework.batch.test.context.SpringBatchTest; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 15 | +import org.springframework.boot.test.context.SpringBootTest; |
| 16 | +import org.springframework.test.context.TestPropertySource; |
| 17 | + |
| 18 | +import java.time.LocalDate; |
| 19 | + |
| 20 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertAll; |
| 22 | + |
| 23 | +@SpringBootTest |
| 24 | +@SpringBatchTest |
| 25 | +@TestPropertySource(properties = "spring.batch.job.name=" + DemoJobConfig.JOB_NAME) |
| 26 | +class DemoJobE2ETest { |
| 27 | + |
| 28 | + // IDE 정적 분석 상 [SpringBatchTest] 의 주입보다 [SpringBootTest] 의 주입이 우선되어, 해당 컴포넌트는 없으므로 오류처럼 보일 수 있음. |
| 29 | + // [SpringBatchTest] 자체가 Scope 기반으로 주입하기 때문에 정상 동작함. |
| 30 | + @Autowired |
| 31 | + private JobLauncherTestUtils jobLauncherTestUtils; |
| 32 | + |
| 33 | + @Autowired |
| 34 | + @Qualifier(DemoJobConfig.JOB_NAME) |
| 35 | + private Job job; |
| 36 | + |
| 37 | + @BeforeEach |
| 38 | + void beforeEach() { |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + @DisplayName("jobParameter 중 requestDate 인자가 주어지지 않았을 때, demoJob 배치는 실패한다.") |
| 43 | + @Test |
| 44 | + void shouldNotSaveCategories_whenApiError() throws Exception { |
| 45 | + // arrange |
| 46 | + jobLauncherTestUtils.setJob(job); |
| 47 | + |
| 48 | + // act |
| 49 | + var jobExecution = jobLauncherTestUtils.launchJob(); |
| 50 | + |
| 51 | + // assert |
| 52 | + assertAll( |
| 53 | + () -> assertThat(jobExecution).isNotNull(), |
| 54 | + () -> assertThat(jobExecution.getExitStatus().getExitCode()).isEqualTo(ExitStatus.FAILED.getExitCode()) |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + @DisplayName("demoJob 배치가 정상적으로 실행된다.") |
| 59 | + @Test |
| 60 | + void success() throws Exception { |
| 61 | + // arrange |
| 62 | + jobLauncherTestUtils.setJob(job); |
| 63 | + |
| 64 | + // act |
| 65 | + var jobParameters = new JobParametersBuilder() |
| 66 | + .addLocalDate("requestDate", LocalDate.now()) |
| 67 | + .toJobParameters(); |
| 68 | + var jobExecution = jobLauncherTestUtils.launchJob(jobParameters); |
| 69 | + |
| 70 | + // assert |
| 71 | + assertAll( |
| 72 | + () -> assertThat(jobExecution).isNotNull(), |
| 73 | + () -> assertThat(jobExecution.getExitStatus().getExitCode()).isEqualTo(ExitStatus.COMPLETED.getExitCode()) |
| 74 | + ); |
| 75 | + } |
| 76 | +} |
0 commit comments