diff --git a/spring-core/src/test/java/org/springframework/core/task/SimpleAsyncTaskExecutorTests.java b/spring-core/src/test/java/org/springframework/core/task/SimpleAsyncTaskExecutorTests.java index a1f4e91f8e2b..fbf1b377fddc 100644 --- a/spring-core/src/test/java/org/springframework/core/task/SimpleAsyncTaskExecutorTests.java +++ b/spring-core/src/test/java/org/springframework/core/task/SimpleAsyncTaskExecutorTests.java @@ -21,6 +21,7 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; import org.junit.jupiter.api.Test; @@ -153,17 +154,18 @@ void taskTerminationTimeout() throws InterruptedException{ @Test void taskTerminationTimeoutWithImmediateCancel() { - AtomicBoolean finished = new AtomicBoolean(); + AtomicReference captured = new AtomicReference<>(); Future future; - try (SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor()) { + try (SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor() { + @Override + protected void doExecute(Runnable task) { + captured.set(task); + } + }) { executor.setTaskTerminationTimeout(100); - future = executor.submit(() -> { - if (finished.get()) { - throw new IllegalStateException(); - } - }); + future = executor.submit(() -> {}); } - finished.set(true); + assertThatExceptionOfType(CancellationException.class).isThrownBy(captured.get()::run); assertThatExceptionOfType(CancellationException.class).isThrownBy(future::get); }