|
| 1 | +package com.baeldung.restartjob; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Path; |
| 8 | +import java.nio.file.StandardCopyOption; |
| 9 | + |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.springframework.batch.core.BatchStatus; |
| 12 | +import org.springframework.batch.core.Job; |
| 13 | +import org.springframework.batch.core.JobExecution; |
| 14 | +import org.springframework.batch.core.JobParameters; |
| 15 | +import org.springframework.batch.core.JobParametersBuilder; |
| 16 | +import org.springframework.batch.core.launch.JobLauncher; |
| 17 | +import org.springframework.batch.test.JobLauncherTestUtils; |
| 18 | +import org.springframework.beans.factory.annotation.Autowired; |
| 19 | +import org.springframework.boot.test.context.SpringBootTest; |
| 20 | +import org.springframework.boot.test.context.TestConfiguration; |
| 21 | +import org.springframework.context.annotation.Bean; |
| 22 | +import org.springframework.context.annotation.Import; |
| 23 | +import org.springframework.core.io.ClassPathResource; |
| 24 | +import org.springframework.core.io.Resource; |
| 25 | + |
| 26 | +@SpringBootTest(classes = {RestartJobBatchApp.class, BatchConfig.class}, |
| 27 | + properties = {"job.autorun.enabled=false"}) |
| 28 | +@Import(RestartJobBatchAppIntegrationTest.TestConfig.class) |
| 29 | +public class RestartJobBatchAppIntegrationTest { |
| 30 | + |
| 31 | + @Autowired |
| 32 | + private JobLauncherTestUtils jobLauncherTestUtils; |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private BatchConfig.RestartItemProcessor itemProcessor; |
| 36 | + |
| 37 | + @TestConfiguration |
| 38 | + static class TestConfig { |
| 39 | + @Autowired |
| 40 | + private JobLauncher jobLauncher; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private Job job; |
| 44 | + |
| 45 | + @Bean |
| 46 | + public JobLauncherTestUtils jobLauncherTestUtils() { |
| 47 | + JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils(); |
| 48 | + jobLauncherTestUtils.setJobLauncher(jobLauncher); |
| 49 | + jobLauncherTestUtils.setJob(job); |
| 50 | + return jobLauncherTestUtils; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private final Resource inputFile = new ClassPathResource("data.csv"); |
| 55 | + |
| 56 | + @Test |
| 57 | + public void givenItems_whenFailed_thenRestartFromFailure() throws Exception { |
| 58 | + // Given |
| 59 | + createTestFile("Item1\nItem2\nItem3\nItem4"); |
| 60 | + |
| 61 | + JobParameters jobParameters = new JobParametersBuilder() |
| 62 | + .addLong("time", System.currentTimeMillis()) |
| 63 | + .toJobParameters(); |
| 64 | + |
| 65 | + // When |
| 66 | + JobExecution firstExecution = jobLauncherTestUtils.launchJob(jobParameters); |
| 67 | + assertEquals(BatchStatus.FAILED, firstExecution.getStatus()); |
| 68 | + |
| 69 | + Long executionId = firstExecution.getId(); |
| 70 | + |
| 71 | + itemProcessor.setFailOnItem3(false); |
| 72 | + |
| 73 | + // Then |
| 74 | + JobExecution restartedExecution = jobLauncherTestUtils.launchJob(jobParameters); |
| 75 | + |
| 76 | + assertEquals(BatchStatus.COMPLETED, restartedExecution.getStatus()); |
| 77 | + |
| 78 | + assertEquals( |
| 79 | + firstExecution.getJobInstance().getInstanceId(), |
| 80 | + restartedExecution.getJobInstance().getInstanceId() |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + private void createTestFile(String content) throws IOException { |
| 85 | + Path tempFile = Files.createTempFile("test-data", ".csv"); |
| 86 | + Files.write(tempFile, content.getBytes()); |
| 87 | + Files.copy(tempFile, inputFile.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING); |
| 88 | + } |
| 89 | + |
| 90 | +} |
| 91 | + |
| 92 | + |
0 commit comments