Skip to content

Commit 783144f

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 (cherry picked from commit ad50599)
1 parent 9f42b40 commit 783144f

File tree

3 files changed

+72
-68
lines changed

3 files changed

+72
-68
lines changed

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

Lines changed: 2 additions & 2 deletions
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.
@@ -104,7 +104,7 @@ public void beforeTestMethod(org.springframework.test.context.TestContext testCo
104104
@Override
105105
public void afterTestMethod(TestContext testContext) throws Exception {
106106
if (testContext.hasAttribute(JOB_EXECUTION)) {
107-
JobSynchronizationManager.close();
107+
JobSynchronizationManager.release();
108108
}
109109
}
110110

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

Lines changed: 3 additions & 2 deletions
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.
@@ -29,6 +29,7 @@
2929
*
3030
* @author Dave Syer
3131
* @author Jimmy Praet
32+
* @author Mahmoud Ben Hassine
3233
*/
3334
public class JobScopeTestUtils {
3435

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

Lines changed: 67 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,67 @@
1-
/*
2-
* Copyright 2013-2018 the original author or authors.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
package org.springframework.batch.test;
17-
18-
import static org.junit.Assert.assertEquals;
19-
import static org.junit.Assert.assertNotNull;
20-
21-
import org.junit.Test;
22-
import org.junit.runner.RunWith;
23-
import org.springframework.batch.core.JobExecution;
24-
import org.springframework.batch.item.ExecutionContext;
25-
import org.springframework.batch.item.ItemReader;
26-
import org.springframework.batch.item.ItemStream;
27-
import org.springframework.beans.factory.annotation.Autowired;
28-
import org.springframework.test.context.ContextConfiguration;
29-
import org.springframework.test.context.TestExecutionListeners;
30-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
31-
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
32-
33-
/**
34-
* @author Dave Syer
35-
* @author Mahmoud Ben Hassine
36-
* @since 2.1
37-
*/
38-
@ContextConfiguration
39-
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, JobScopeTestExecutionListener.class })
40-
@RunWith(SpringJUnit4ClassRunner.class)
41-
public class JobScopeTestExecutionListenerIntegrationTests {
42-
43-
@Autowired
44-
private ItemReader<String> reader;
45-
46-
@Autowired
47-
private ItemStream stream;
48-
49-
public JobExecution getJobExecution() {
50-
// Assert that dependencies are already injected...
51-
assertNotNull(reader);
52-
// Then create the execution for the job scope...
53-
JobExecution execution = MetaDataInstanceFactory.createJobExecution();
54-
execution.getExecutionContext().putString("input.file", "classpath:/org/springframework/batch/test/simple.txt");
55-
return execution;
56-
}
57-
58-
@Test
59-
public void testJob() throws Exception {
60-
stream.open(new ExecutionContext());
61-
assertEquals("foo", reader.read());
62-
}
63-
64-
}
1+
/*
2+
* Copyright 2013-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.test;
17+
18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.Assert.assertNull;
21+
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.springframework.batch.core.JobExecution;
25+
import org.springframework.batch.item.ExecutionContext;
26+
import org.springframework.batch.item.ItemReader;
27+
import org.springframework.batch.item.ItemStream;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.test.context.ContextConfiguration;
30+
import org.springframework.test.context.TestExecutionListeners;
31+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32+
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
33+
34+
/**
35+
* @author Dave Syer
36+
* @author Mahmoud Ben Hassine
37+
* @since 2.1
38+
*/
39+
@ContextConfiguration
40+
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, JobScopeTestExecutionListener.class })
41+
@RunWith(SpringJUnit4ClassRunner.class)
42+
public class JobScopeTestExecutionListenerIntegrationTests {
43+
44+
@Autowired
45+
private ItemReader<String> reader;
46+
47+
@Autowired
48+
private ItemStream stream;
49+
50+
public JobExecution getJobExecution() {
51+
// Assert that dependencies are already injected...
52+
assertNotNull(reader);
53+
// Then create the execution for the job scope...
54+
JobExecution execution = MetaDataInstanceFactory.createJobExecution();
55+
execution.getExecutionContext().putString("input.file", "classpath:/org/springframework/batch/test/simple.txt");
56+
return execution;
57+
}
58+
59+
@Test
60+
public void testJob() throws Exception {
61+
stream.open(new ExecutionContext());
62+
assertEquals("foo", reader.read());
63+
assertEquals("bar", reader.read());
64+
assertNull(reader.read());
65+
}
66+
67+
}

0 commit comments

Comments
 (0)