Skip to content

Commit ad50599

Browse files
committed
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
1 parent 03d2833 commit ad50599

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

spring-batch-test/src/main/java/org/springframework/batch/test/JobScopeTestExecutionListener.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void beforeTestMethod(org.springframework.test.context.TestContext testCo
100100
@Override
101101
public void afterTestMethod(TestContext testContext) {
102102
if (testContext.hasAttribute(JOB_EXECUTION)) {
103-
JobSynchronizationManager.close();
103+
JobSynchronizationManager.release();
104104
}
105105
}
106106

spring-batch-test/src/main/java/org/springframework/batch/test/JobScopeTestUtils.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2010 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
*
2929
* @author Dave Syer
3030
* @author Jimmy Praet
31+
* @author Mahmoud Ben Hassine
3132
*/
3233
public class JobScopeTestUtils {
3334

@@ -37,7 +38,7 @@ public static <T> T doInJobScope(JobExecution jobExecution, Callable<T> callable
3738
return callable.call();
3839
}
3940
finally {
40-
JobSynchronizationManager.close();
41+
JobSynchronizationManager.release();
4142
}
4243
}
4344

spring-batch-test/src/test/java/org/springframework/batch/test/JobScopeTestExecutionListenerIntegrationTests.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717

1818
import static org.junit.jupiter.api.Assertions.assertEquals;
1919
import static org.junit.jupiter.api.Assertions.assertNotNull;
20+
import static org.junit.jupiter.api.Assertions.assertNull;
2021

2122
import org.junit.jupiter.api.Test;
2223
import org.springframework.batch.core.JobExecution;
@@ -56,6 +57,8 @@ JobExecution getJobExecution() {
5657
void testJob() throws Exception {
5758
stream.open(new ExecutionContext());
5859
assertEquals("foo", reader.read());
60+
assertEquals("bar", reader.read());
61+
assertNull(reader.read());
5962
}
6063

6164
}

0 commit comments

Comments
 (0)