|
3 | 3 | import org.junit.Before;
|
4 | 4 | import org.junit.Test;
|
5 | 5 | import org.junit.runner.RunWith;
|
| 6 | +import org.springframework.batch.core.BatchStatus; |
| 7 | +import org.springframework.batch.core.ExitStatus; |
6 | 8 | import org.springframework.batch.core.JobExecution;
|
7 | 9 | import org.springframework.batch.core.JobParameters;
|
8 | 10 | import org.springframework.batch.core.Step;
|
|
22 | 24 | import org.springframework.transaction.PlatformTransactionManager;
|
23 | 25 |
|
24 | 26 | import java.util.ArrayList;
|
| 27 | +import java.util.Arrays; |
25 | 28 | import java.util.List;
|
26 | 29 |
|
27 | 30 | import static org.junit.Assert.assertEquals;
|
@@ -139,7 +142,55 @@ public void testFilterCountOnRetryWithNonTransactionalProcessorWhenSkipInWrite()
|
139 | 142 | assertEquals(19, stepExecution.getWriteCount());
|
140 | 143 | assertEquals(1, stepExecution.getWriteSkipCount());
|
141 | 144 | }
|
142 |
| - |
| 145 | + |
| 146 | + @Test(timeout = 3000) |
| 147 | + public void testExceptionInProcessDuringChunkScan() throws Exception { |
| 148 | + // Given |
| 149 | + ListItemReader<Integer> itemReader = new ListItemReader<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7)); |
| 150 | + ItemProcessor<Integer, Integer> itemProcessor = new ItemProcessor<Integer, Integer>() { |
| 151 | + int cpt; |
| 152 | + |
| 153 | + @Override |
| 154 | + public Integer process(Integer item) throws Exception { |
| 155 | + cpt++; |
| 156 | + if (cpt == 7) { // item 2 succeeds the first time but fails during the scan |
| 157 | + throw new Exception("Error during process"); |
| 158 | + } |
| 159 | + return item; |
| 160 | + } |
| 161 | + }; |
| 162 | + ItemWriter<Integer> itemWriter = new ItemWriter<Integer>() { |
| 163 | + int cpt; |
| 164 | + |
| 165 | + @Override |
| 166 | + public void write(List<? extends Integer> items) throws Exception { |
| 167 | + cpt++; |
| 168 | + if (cpt == 1) { |
| 169 | + throw new Exception("Error during write"); |
| 170 | + } |
| 171 | + } |
| 172 | + }; |
| 173 | + Step step = new StepBuilderFactory(jobRepository, transactionManager).get("step") |
| 174 | + .<Integer, Integer>chunk(5) |
| 175 | + .reader(itemReader) |
| 176 | + .processor(itemProcessor) |
| 177 | + .writer(itemWriter) |
| 178 | + .faultTolerant() |
| 179 | + .skip(Exception.class) |
| 180 | + .skipLimit(3) |
| 181 | + .build(); |
| 182 | + |
| 183 | + // When |
| 184 | + StepExecution stepExecution = execute(step); |
| 185 | + |
| 186 | + // Then |
| 187 | + assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus()); |
| 188 | + assertEquals(ExitStatus.COMPLETED, stepExecution.getExitStatus()); |
| 189 | + assertEquals(7, stepExecution.getReadCount()); |
| 190 | + assertEquals(6, stepExecution.getWriteCount()); |
| 191 | + assertEquals(1, stepExecution.getProcessSkipCount()); |
| 192 | + } |
| 193 | + |
143 | 194 | private List<Integer> createItems() {
|
144 | 195 | List<Integer> items = new ArrayList<>(TOTAL_ITEMS);
|
145 | 196 | for (int i = 1; i <= TOTAL_ITEMS; i++) {
|
|
0 commit comments