Skip to content

Commit ba653fe

Browse files
committed
#BAEL-8695: add unit test
1 parent 1a99961 commit ba653fe

File tree

3 files changed

+116
-11
lines changed

3 files changed

+116
-11
lines changed

spring-batch-2/src/main/java/com/baeldung/restartjob/BatchConfig.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,8 @@ public FlatFileItemReader<String> flatFileItemReader() {
4848
}
4949

5050
@Bean
51-
public ItemProcessor<String, String> itemProcessor() {
52-
return item -> {
53-
System.out.println("Processing: " + item);
54-
55-
if (item.equals("Item3")) {
56-
throw new RuntimeException("Simulated failure on Item3");
57-
}
58-
59-
return "PROCESSED " + item;
60-
};
51+
public RestartItemProcessor itemProcessor() {
52+
return new RestartItemProcessor();
6153
}
6254

6355
@Bean
@@ -69,4 +61,21 @@ public ItemWriter<String> itemWriter() {
6961
}
7062
};
7163
}
64+
65+
static class RestartItemProcessor implements ItemProcessor<String, String> {
66+
private boolean failOnItem3 = true;
67+
68+
public void setFailOnItem3(boolean failOnItem3) {
69+
this.failOnItem3 = failOnItem3;
70+
}
71+
72+
@Override
73+
public String process(String item) throws Exception {
74+
System.out.println("Processing: " + item + " (failOnItem3=" + failOnItem3 + ")");
75+
if (failOnItem3 && item.equals("Item3")) {
76+
throw new RuntimeException("Simulated failure on Item3");
77+
}
78+
return "PROCESSED " + item;
79+
}
80+
}
7281
}

spring-batch-2/src/main/java/com/baeldung/restartjob/RestartJobBatchApp.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.springframework.boot.CommandLineRunner;
1515
import org.springframework.boot.SpringApplication;
1616
import org.springframework.boot.autoconfigure.SpringBootApplication;
17+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
1718
import org.springframework.context.annotation.Bean;
1819

1920
@SpringBootApplication
@@ -26,7 +27,9 @@ public static void main(String[] args) {
2627
}
2728

2829
@Bean
29-
CommandLineRunner run(JobLauncher jobLauncher, Job job, JobExplorer jobExplorer, JobOperator jobOperator) {
30+
@ConditionalOnProperty(prefix = "job.autorun", name = "enabled", havingValue = "true", matchIfMissing = true)
31+
CommandLineRunner run(JobLauncher jobLauncher, Job job, JobExplorer jobExplorer,
32+
JobOperator jobOperator, BatchConfig.RestartItemProcessor itemProcessor) {
3033
return args -> {
3134
JobParameters jobParameters = new JobParametersBuilder()
3235
.addString("jobId", "test-job-" + System.currentTimeMillis())
@@ -40,6 +43,7 @@ CommandLineRunner run(JobLauncher jobLauncher, Job job, JobExplorer jobExplorer,
4043
JobExecution lastExecution = executions.get(0);
4144
if (lastExecution.getStatus() == BatchStatus.FAILED) {
4245
System.out.println("Restarting failed job execution with ID: " + lastExecution.getId());
46+
itemProcessor.setFailOnItem3(false);
4347

4448
JobExecution restartedExecution = jobLauncher.run(job, jobParameters);
4549

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)