-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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)
- Loading branch information
1 parent
9f42b40
commit 783144f
Showing
3 changed files
with
72 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 67 additions & 64 deletions
131
...st/java/org/springframework/batch/test/JobScopeTestExecutionListenerIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,67 @@ | ||
/* | ||
* Copyright 2013-2018 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.batch.test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.batch.core.JobExecution; | ||
import org.springframework.batch.item.ExecutionContext; | ||
import org.springframework.batch.item.ItemReader; | ||
import org.springframework.batch.item.ItemStream; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.TestExecutionListeners; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; | ||
|
||
/** | ||
* @author Dave Syer | ||
* @author Mahmoud Ben Hassine | ||
* @since 2.1 | ||
*/ | ||
@ContextConfiguration | ||
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, JobScopeTestExecutionListener.class }) | ||
@RunWith(SpringJUnit4ClassRunner.class) | ||
public class JobScopeTestExecutionListenerIntegrationTests { | ||
|
||
@Autowired | ||
private ItemReader<String> reader; | ||
|
||
@Autowired | ||
private ItemStream stream; | ||
|
||
public JobExecution getJobExecution() { | ||
// Assert that dependencies are already injected... | ||
assertNotNull(reader); | ||
// Then create the execution for the job scope... | ||
JobExecution execution = MetaDataInstanceFactory.createJobExecution(); | ||
execution.getExecutionContext().putString("input.file", "classpath:/org/springframework/batch/test/simple.txt"); | ||
return execution; | ||
} | ||
|
||
@Test | ||
public void testJob() throws Exception { | ||
stream.open(new ExecutionContext()); | ||
assertEquals("foo", reader.read()); | ||
} | ||
|
||
} | ||
/* | ||
* 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.batch.test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.batch.core.JobExecution; | ||
import org.springframework.batch.item.ExecutionContext; | ||
import org.springframework.batch.item.ItemReader; | ||
import org.springframework.batch.item.ItemStream; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.TestExecutionListeners; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; | ||
|
||
/** | ||
* @author Dave Syer | ||
* @author Mahmoud Ben Hassine | ||
* @since 2.1 | ||
*/ | ||
@ContextConfiguration | ||
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, JobScopeTestExecutionListener.class }) | ||
@RunWith(SpringJUnit4ClassRunner.class) | ||
public class JobScopeTestExecutionListenerIntegrationTests { | ||
|
||
@Autowired | ||
private ItemReader<String> reader; | ||
|
||
@Autowired | ||
private ItemStream stream; | ||
|
||
public JobExecution getJobExecution() { | ||
// Assert that dependencies are already injected... | ||
assertNotNull(reader); | ||
// Then create the execution for the job scope... | ||
JobExecution execution = MetaDataInstanceFactory.createJobExecution(); | ||
execution.getExecutionContext().putString("input.file", "classpath:/org/springframework/batch/test/simple.txt"); | ||
return execution; | ||
} | ||
|
||
@Test | ||
public void testJob() throws Exception { | ||
stream.open(new ExecutionContext()); | ||
assertEquals("foo", reader.read()); | ||
assertEquals("bar", reader.read()); | ||
assertNull(reader.read()); | ||
} | ||
|
||
} |