From 55a433cf2c2322940ca24c7344e7754ecdce70ac Mon Sep 17 00:00:00 2001 From: sumittlearnbay Date: Tue, 21 Jul 2026 20:27:49 +0530 Subject: [PATCH 1/2] Update file deletion tests after Files.delete migration Signed-off-by: sumittlearnbay --- .../item/xml/StaxEventItemWriterTests.java | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/infrastructure/item/xml/StaxEventItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/infrastructure/item/xml/StaxEventItemWriterTests.java index f0a5f54b7f..8671568ecd 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/infrastructure/item/xml/StaxEventItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/infrastructure/item/xml/StaxEventItemWriterTests.java @@ -17,6 +17,7 @@ import java.io.File; import java.io.IOException; +import java.io.RandomAccessFile; import java.util.Collections; import javax.xml.stream.XMLEventFactory; @@ -832,19 +833,29 @@ void testOpenAndCloseTagsInComplexCallbacksRestart() throws Exception { * indicate the deletion attempt failed. */ @Test - void testFailedFileDeletionThrowsException() throws IOException { - File mockedFile = spy(resource.getFile()); - writer.setResource(new FileSystemResource(mockedFile)); + void testFailedFileDeletionThrowsException() throws Exception { + + File outputFile = resource.getFile(); + + writer.setResource(new FileSystemResource(outputFile)); writer.setShouldDeleteIfEmpty(true); writer.open(executionContext); - when(mockedFile.delete()).thenReturn(false); + // Keep the file locked so Files.delete(...) fails on Windows + RandomAccessFile lock = new RandomAccessFile(outputFile, "rw"); + + try { + ItemStreamException exception = assertThrows(ItemStreamException.class, writer::close, + "Expected exception when file deletion fails"); - ItemStreamException exception = assertThrows(ItemStreamException.class, () -> writer.close(), - "Expected exception when file deletion fails"); + assertEquals("Failed to delete empty file on close", exception.getMessage()); - assertEquals("Failed to delete empty file on close", exception.getMessage(), "Wrong exception message"); - assertNotNull(exception.getCause(), "Exception should have a cause"); + assertNotNull(exception.getCause()); + assertTrue(exception.getCause() instanceof IOException); + } + finally { + lock.close(); + } } private void initWriterForSimpleCallbackTests() throws Exception { From 3fe9c2b2db3fce3b51df3db6856788ac186b0ea3 Mon Sep 17 00:00:00 2001 From: sumittlearnbay Date: Tue, 21 Jul 2026 20:29:16 +0530 Subject: [PATCH 2/2] Update AbstractFileItemWriter deletion test Signed-off-by: sumittlearnbay --- .../support/AbstractFileItemWriterTest.java | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/infrastructure/item/support/AbstractFileItemWriterTest.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/infrastructure/item/support/AbstractFileItemWriterTest.java index e1e0c56f5e..125c79acf5 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/infrastructure/item/support/AbstractFileItemWriterTest.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/infrastructure/item/support/AbstractFileItemWriterTest.java @@ -16,12 +16,12 @@ package org.springframework.batch.infrastructure.item.support; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.when; import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -39,24 +39,32 @@ class AbstractFileItemWriterTests { @Test - void testFailedFileDeletionThrowsException() { + void testFailedFileDeletionThrowsException() throws Exception { + File outputFile = new File("target/data/output.tmp"); - File mocked = Mockito.spy(outputFile); + outputFile.getParentFile().mkdirs(); TestFileItemWriter writer = new TestFileItemWriter(); - - writer.setResource(new FileSystemResource(mocked)); + writer.setResource(new FileSystemResource(outputFile)); writer.setShouldDeleteIfEmpty(true); writer.setName(writer.getClass().getSimpleName()); + writer.open(new ExecutionContext()); - when(mocked.delete()).thenReturn(false); + // Keep the file open so Files.delete(...) cannot delete it (Windows) + RandomAccessFile lock = new RandomAccessFile(outputFile, "rw"); - ItemStreamException exception = assertThrows(ItemStreamException.class, writer::close, - "Expected exception when file deletion fails"); + try { + ItemStreamException exception = assertThrows(ItemStreamException.class, writer::close); - assertEquals("Failed to delete empty file on close", exception.getMessage(), "Wrong exception message"); - assertNotNull(exception.getCause(), "Exception should have a cause"); + assertEquals("Failed to delete empty file on close", exception.getMessage()); + + assertNotNull(exception.getCause()); + assertTrue(exception.getCause() instanceof IOException); + } + finally { + lock.close(); + } } private static class TestFileItemWriter extends AbstractFileItemWriter {