DeleteFileTasklet implements Tasklet, InitializingBean { @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { List<Resource> files = resources != null ? Arrays.asList(resources) : Collections.EMPTY_LIST; files.stream().flatMap(resource -> { Stream<File> result = Stream.<File>empty(); LOGGER.info("Deleting file {}...", resource.getFilename()); try { result = Stream.of(resource.getFile()); } catch (IOException e) { LOGGER.error("Cannot retrieve the File from this Resource", e); } return result; }).filter(file -> { contribution.incrementWriteCount(1); return !file.delete(); }).forEach(File::deleteOnExit); return RepeatStatus.FINISHED; }  @Override void afterPropertiesSet(); @Override RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext); void setResources(Resource[] resources);  }
@Test public void testExecute() throws Exception { Assert.assertEquals(1, getExcelFiles().length); StepExecution stepExecution = MetaDataInstanceFactory.createStepExecution(); StepContribution stepContribution = new StepContribution(stepExecution); ChunkContext chunkContext = new ChunkContext(new StepContext(stepExecution)); RepeatStatus repeatStatus = tasklet.execute(stepContribution, chunkContext); Assert.assertEquals(RepeatStatus.FINISHED, repeatStatus); Assert.assertEquals(0, getExcelFiles().length); Assert.assertEquals(1, stepContribution.getWriteCount()); }