From ad50599d280b903f152ea1460eab0f1a25eac0b7 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Tue, 21 Nov 2023 09:14:18 +0100 Subject: [PATCH] Fix disposable bean lifecycle in JobScope test utilities Before this commit, the destroy method of a job-scoped bean was not called after a test method. This commit changes the listener to respect the DisposableBean contract for job-scoped beans (and make it consistent with the calls to the JobSynchronizationManager in AbstractJob, ie calling register/release). FTR, I did not find a clean way to test this with an assertion (which should be made after the test method), but a log message in the destroy method shows that the method is now called as expected. Resolves #1288 --- .../batch/test/JobScopeTestExecutionListener.java | 2 +- .../org/springframework/batch/test/JobScopeTestUtils.java | 5 +++-- .../test/JobScopeTestExecutionListenerIntegrationTests.java | 5 ++++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/JobScopeTestExecutionListener.java b/spring-batch-test/src/main/java/org/springframework/batch/test/JobScopeTestExecutionListener.java index 249fb8636b..5376d5a87a 100644 --- a/spring-batch-test/src/main/java/org/springframework/batch/test/JobScopeTestExecutionListener.java +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/JobScopeTestExecutionListener.java @@ -100,7 +100,7 @@ public void beforeTestMethod(org.springframework.test.context.TestContext testCo @Override public void afterTestMethod(TestContext testContext) { if (testContext.hasAttribute(JOB_EXECUTION)) { - JobSynchronizationManager.close(); + JobSynchronizationManager.release(); } } diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/JobScopeTestUtils.java b/spring-batch-test/src/main/java/org/springframework/batch/test/JobScopeTestUtils.java index 00e9871fe5..989512bb46 100644 --- a/spring-batch-test/src/main/java/org/springframework/batch/test/JobScopeTestUtils.java +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/JobScopeTestUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2010 the original author or authors. + * Copyright 2006-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ * * @author Dave Syer * @author Jimmy Praet + * @author Mahmoud Ben Hassine */ public class JobScopeTestUtils { @@ -37,7 +38,7 @@ public static T doInJobScope(JobExecution jobExecution, Callable callable return callable.call(); } finally { - JobSynchronizationManager.close(); + JobSynchronizationManager.release(); } } diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/JobScopeTestExecutionListenerIntegrationTests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/JobScopeTestExecutionListenerIntegrationTests.java index 707732bba7..d295007f00 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/JobScopeTestExecutionListenerIntegrationTests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/JobScopeTestExecutionListenerIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2022 the original author or authors. + * Copyright 2013-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import org.junit.jupiter.api.Test; import org.springframework.batch.core.JobExecution; @@ -56,6 +57,8 @@ JobExecution getJobExecution() { void testJob() throws Exception { stream.open(new ExecutionContext()); assertEquals("foo", reader.read()); + assertEquals("bar", reader.read()); + assertNull(reader.read()); } }