<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.batch:spring-batch-test'
}
@RunWith(SpringRunner.class)
@SpringBatchTest
@SpringBootTest(classes={BatchJpaTestConfiguration.class, TestBatchConfig.class})
public class BatchIntegrationTestJobConfigurationTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils; // (2)
@Autowired
private SalesRepository salesRepository;
@Autowired
private SalesSumRepository salesSumRepository;
@After
public void tearDown() throws Exception {
salesRepository.deleteAllInBatch();
salesSumRepository.deleteAllInBatch();
}
@Test
public void 기간내_Sales가_집계되어_SalesSum이된다() throws Exception {
//given
LocalDate orderDate = LocalDate.of(2019,10,6);
int amount1 = 1000;
int amount2 = 500;
int amount3 = 100;
salesRepository.save(new Sales(orderDate, amount1, "1"));
salesRepository.save(new Sales(orderDate, amount2, "2"));
salesRepository.save(new Sales(orderDate, amount3, "3"));
JobParameters jobParameters = new JobParametersBuilder()
.addString("orderDate", orderDate.format(FORMATTER))
.toJobParameters();
//when
JobExecution jobExecution = jobLauncherTestUtils.launchJob(jobParameters); // (3)
//then
assertThat(jobExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED);
List<SalesSum> salesSumList = salesSumRepository.findAll();
assertThat(salesSumList.size()).isEqualTo(1);
assertThat(salesSumList.get(0).getOrderDate()).isEqualTo(orderDate);
assertThat(salesSumList.get(0).getAmountSum()).isEqualTo(amount1+amount2+amount3);
}
}
@Configuration
@EnableAutoConfiguration
@EnableBatchProcessing // 배치 환경을 자동 설정
public class TestBatchConfig {
}