Remove job autowiring in JobLauncherTestUtils

Before this commit, registering the JobLauncherTestUtils
as a bean in the test context (either manually or via
`@SpringBatchTest`) was failing when multiple jobs are
defined in the test context.

This commit removes the autowiring of the job under test
in JobLauncherTestUtils.

Resolves #1237
This commit is contained in:
Mahmoud Ben Hassine
2022-08-20 12:00:08 +02:00
parent e67c0069f1
commit c506a49988
37 changed files with 195 additions and 86 deletions

View File

@@ -22,20 +22,15 @@ approach.
For the unit test to run a batch job, the framework must load the job's
`ApplicationContext`. Two annotations are used to trigger this behavior:
* `@RunWith(SpringJUnit4ClassRunner.class)` indicates that the class should use Spring's
* `@SpringJUnitConfig` indicates that the class should use Spring's
JUnit facilities
* `@ContextConfiguration(...)` indicates which resources to configure the
`ApplicationContext` with.
* `@SpringBatchTest` injects Spring Batch test utilities (such as the
`JobLauncherTestUtils` and `JobRepositoryTestUtils`) in the test context
Starting from v4.1, it is also possible to inject Spring Batch test utilities
(such as the `JobLauncherTestUtils` and `JobRepositoryTestUtils`) in the test context
by using the `@SpringBatchTest` annotation.
NOTE: Note that `JobLauncherTestUtils` requires a `Job` bean and that
`JobRepositoryTestUtils` requires a `DataSource` bean. Since `@SpringBatchTest`
registers a `JobLauncherTestUtils` and a `JobRepositoryTestUtils` in the test
NOTE: Note that `JobRepositoryTestUtils` requires a `DataSource` bean. Since
`@SpringBatchTest` registers a `JobRepositoryTestUtils` in the test
context, it is expected that the test context contains a single autowire candidate
for a `Job` and a `DataSource` (either a single bean definition or one that is
for a `DataSource` (either a single bean definition or one that is
annotated with `org.springframework.context.annotation.Primary`).
[role="javaContent"]
@@ -45,8 +40,7 @@ The following Java example shows the annotations in use:
[source, java, role="javaContent"]
----
@SpringBatchTest
@RunWith(SpringRunner.class)
@ContextConfiguration(classes=SkipSampleConfiguration.class)
@SpringJUnitConfig(SkipSampleConfiguration.class)
public class SkipSampleFunctionalTests { ... }
----
@@ -57,8 +51,7 @@ The following XML example shows the annotations in use:
[source, java, role="xmlContent"]
----
@SpringBatchTest
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml",
@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml",
"/jobs/skipSampleJob.xml" })
public class SkipSampleFunctionalTests { ... }
----
@@ -81,14 +74,13 @@ about the `Job` run. In the following case, the test verifies that the `Job` end
a status of `COMPLETED`.
[role="xmlContent"]
The following listing shows the example in XML:
The following listing shows an example with JUnit 5 in XML configuration style:
.XML Based Configuration
[source, java, role="xmlContent"]
----
@SpringBatchTest
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml",
@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml",
"/jobs/skipSampleJob.xml" })
public class SkipSampleFunctionalTests {
@@ -103,7 +95,8 @@ public class SkipSampleFunctionalTests {
}
@Test
public void testJob() throws Exception {
public void testJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
simpleJdbcTemplate.update("delete from CUSTOMER");
for (int i = 1; i <= 10; i++) {
simpleJdbcTemplate.update("insert into CUSTOMER values (?, 0, ?, 100000)",
@@ -119,14 +112,13 @@ public class SkipSampleFunctionalTests {
----
[role="javaContent"]
The following listing shows the example in Java:
The following listing shows an example with JUnit 5 in Java configuration style:
.Java Based Configuration
[source, java, role="javaContent"]
----
@SpringBatchTest
@RunWith(SpringRunner.class)
@ContextConfiguration(classes=SkipSampleConfiguration.class)
@SpringJUnitConfig(SkipSampleConfiguration.class)
public class SkipSampleFunctionalTests {
@Autowired
@@ -140,7 +132,8 @@ public class SkipSampleFunctionalTests {
}
@Test
public void testJob() throws Exception {
public void testJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
simpleJdbcTemplate.update("delete from CUSTOMER");
for (int i = 1; i <= 10; i++) {
simpleJdbcTemplate.update("insert into CUSTOMER values (?, 0, ?, 100000)",
@@ -186,10 +179,9 @@ context for each test method, as the following example shows:
[source, java]
----
@ContextConfiguration
@SpringJUnitConfig
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class,
StepScopeTestExecutionListener.class })
@RunWith(SpringRunner.class)
public class StepScopeTestExecutionListenerIntegrationTests {
// This component is defined step-scoped, so it cannot be injected unless
@@ -228,8 +220,7 @@ example can be configured as follows:
[source, java]
----
@SpringBatchTest
@RunWith(SpringRunner.class)
@ContextConfiguration
@SpringJUnitConfig
public class StepScopeTestExecutionListenerIntegrationTests {
// This component is defined step-scoped, so it cannot be injected unless

View File

@@ -18,6 +18,8 @@ package org.springframework.batch.sample;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -48,12 +50,15 @@ class AMQPJobFunctionalTests {
private JobExplorer jobExplorer;
@Test
void testLaunchJob() throws Exception {
jobLauncherTestUtils.launchJob();
void testLaunchJob(@Autowired Job job) throws Exception {
// given
this.jobLauncherTestUtils.setJob(job);
this.jobLauncherTestUtils.launchJob();
// when
int count = jobExplorer.getJobInstances("amqp-example-job", 0, 1).size();
// then
assertTrue(count > 0);
}

View File

@@ -17,6 +17,8 @@
package org.springframework.batch.sample;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -29,10 +31,15 @@ class BeanWrapperMapperSampleJobFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testJobLaunch() throws Exception {
void testJobLaunch(@Autowired Job job) throws Exception {
// given
this.jobLauncherTestUtils.setJob(job);
jobLauncherTestUtils.launchJob();
// when
this.jobLauncherTestUtils.launchJob();
// then
// FIXME no assertions?
}
}

View File

@@ -27,6 +27,7 @@ import javax.sql.DataSource;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -60,7 +61,8 @@ class CompositeItemWriterSampleFunctionalTests {
}
@Test
void testJobLaunch() throws Exception {
void testJobLaunch(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE");
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE");

View File

@@ -30,6 +30,8 @@ import javax.sql.DataSource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -80,7 +82,8 @@ class CustomerFilterJobFunctionalTests {
}
@Test
void testFilterJob() throws Exception {
void testFilterJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
customers = Arrays.asList(new Customer("customer1", (credits.get("customer1"))),

View File

@@ -25,6 +25,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.launch.JobOperator;
import org.springframework.batch.test.JobLauncherTestUtils;
@@ -53,8 +54,8 @@ class DatabaseShutdownFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testLaunchJob() throws Exception {
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
Thread.sleep(1000);

View File

@@ -19,6 +19,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.sample.domain.person.PersonService;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -35,8 +37,8 @@ class DelegatingJobFunctionalTests {
private PersonService personService;
@Test
void testLaunchJob() throws Exception {
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
jobLauncherTestUtils.launchJob();
assertTrue(personService.getReturnedCount() > 0);

View File

@@ -20,6 +20,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
@@ -41,7 +43,8 @@ class FootballJobFunctionalTests {
}
@Test
void testLaunchJob() throws Exception {
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
JdbcTestUtils.deleteFromTables(jdbcTemplate, "PLAYERS", "GAMES", "PLAYER_SUMMARY");
jobLauncherTestUtils.launchJob();

View File

@@ -24,6 +24,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
@@ -54,8 +55,8 @@ class GracefulShutdownFunctionalTests {
private JobOperator jobOperator;
@Test
void testLaunchJob() throws Exception {
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
final JobParameters jobParameters = new JobParametersBuilder().addLong("timestamp", System.currentTimeMillis())
.toJobParameters();

View File

@@ -25,6 +25,8 @@ import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -41,7 +43,8 @@ class GroovyJobFunctionalTests {
}
@Test
void testLaunchJob() throws Exception {
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
assertFalse(new File("target/groovyJob/output/files.zip").exists());
jobLauncherTestUtils.launchJob();
assertTrue(new File("target/groovyJob/output/files.zip").exists());

View File

@@ -21,6 +21,8 @@ import java.io.BufferedReader;
import java.io.FileReader;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -43,8 +45,9 @@ class HeaderFooterSampleFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testJob() throws Exception {
jobLauncherTestUtils.launchJob();
void testJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
this.jobLauncherTestUtils.launchJob();
BufferedReader inputReader = new BufferedReader(new FileReader(input.getFile()));
BufferedReader outputReader = new BufferedReader(new FileReader(output.getFile()));

View File

@@ -28,6 +28,7 @@ import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor;
@@ -89,7 +90,8 @@ class HibernateFailureJobFunctionalTests {
}
@Test
void testLaunchJob() throws Exception {
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
validatePreConditions();
JobParameters params = new JobParametersBuilder().addString("key", "failureJob").toJobParameters();

View File

@@ -18,6 +18,8 @@ package org.springframework.batch.sample;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.sample.domain.trade.internal.ItemTrackingTradeItemWriter;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -40,8 +42,9 @@ class LoopFlowSampleFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testJobLaunch() throws Exception {
jobLauncherTestUtils.launchJob();
void testJobLaunch(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
this.jobLauncherTestUtils.launchJob();
// items processed = items read + 2 exceptions
assertEquals(10, itemWriter.getItems().size());
}

View File

@@ -26,6 +26,7 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.sample.domain.mail.internal.TestMailErrorHandler;
import org.springframework.batch.sample.domain.mail.internal.TestMailSender;
@@ -93,7 +94,8 @@ class MailJobFunctionalTests {
}
@Test
void testSkip() throws Exception {
void testSkip(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
this.createUsers(new Object[][] { USER1, USER2_SKIP, USER3, USER4_SKIP, USER5, USER6, USER7, USER8 });
JobExecution jobExecution = jobLauncherTestUtils.launchJob();

View File

@@ -19,6 +19,7 @@ package org.springframework.batch.sample;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
@@ -43,8 +44,9 @@ class MultilineJobFunctionalTests {
private final Resource output = new FileSystemResource("target/test-outputs/20070122.testStream.multilineStep.txt");
@Test
void testJobLaunch() throws Exception {
jobLauncherTestUtils.launchJob();
void testJobLaunch(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
this.jobLauncherTestUtils.launchJob();
assertEquals(EXPECTED_RESULT, StringUtils.replace(IOUtils.toString(output.getInputStream(), "UTF-8"),
System.getProperty("line.separator"), ""));
}

View File

@@ -19,6 +19,8 @@ package org.springframework.batch.sample;
import static org.springframework.batch.test.AssertFile.assertFileEquals;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
@@ -37,8 +39,9 @@ class MultilineOrderJobFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testJobLaunch() throws Exception {
jobLauncherTestUtils.launchJob();
void testJobLaunch(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
this.jobLauncherTestUtils.launchJob();
assertFileEquals(new ClassPathResource(EXPECTED), new FileSystemResource(ACTUAL));
}

View File

@@ -22,6 +22,7 @@ import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.batch.test.JobLauncherTestUtils;
@@ -44,7 +45,8 @@ class ParallelJobFunctionalTests {
}
@Test
void testLaunchJob() throws Exception {
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STAGING");
JobExecution execution = jobLauncherTestUtils.launchJob();
int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STAGING");

View File

@@ -26,6 +26,7 @@ import java.util.Set;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
@@ -62,7 +63,8 @@ class PartitionFileJobFunctionalTests implements ApplicationContextAware {
* Check the resulting credits correspond to inputs increased by fixed amount.
*/
@Test
void testUpdateCredit() throws Exception {
void testUpdateCredit(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
assertTrue(applicationContext.containsBeanDefinition("outputTestReader"),
"Define a prototype bean called 'outputTestReader' to check the output");

View File

@@ -26,6 +26,7 @@ import java.util.Set;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
@@ -62,7 +63,8 @@ class PartitionJdbcJobFunctionalTests implements ApplicationContextAware {
* Check the resulting credits correspond to inputs increased by fixed amount.
*/
@Test
void testUpdateCredit() throws Exception {
void testUpdateCredit(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
assertTrue(applicationContext.containsBeanDefinition("outputTestReader"),
"Define a prototype bean called 'outputTestReader' to check the output");

View File

@@ -23,6 +23,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.sample.config.JobRunnerConfiguration;
import org.springframework.batch.sample.remotechunking.ManagerConfiguration;
@@ -69,7 +70,10 @@ class RemoteChunkingJobFunctionalTests {
}
@Test
void testRemoteChunkingJob() throws Exception {
void testRemoteChunkingJob(@Autowired Job job) throws Exception {
// given
this.jobLauncherTestUtils.setJob(job);
// when
JobExecution jobExecution = this.jobLauncherTestUtils.launchJob();

View File

@@ -25,6 +25,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -75,7 +76,10 @@ public abstract class RemotePartitioningJobFunctionalTests {
}
@Test
void testRemotePartitioningJob() throws Exception {
void testRemotePartitioningJob(@Autowired Job job) throws Exception {
// given
this.jobLauncherTestUtils.setJob(job);
// when
JobExecution jobExecution = this.jobLauncherTestUtils.launchJob();

View File

@@ -22,6 +22,7 @@ import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.item.Chunk;
@@ -49,7 +50,8 @@ class RestartFileSampleFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void runTest() throws Exception {
void runTest(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
JobParameters jobParameters = jobLauncherTestUtils.getUniqueJobParameters();
JobExecution je1 = jobLauncherTestUtils.launchJob(jobParameters);

View File

@@ -23,6 +23,7 @@ import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
import org.springframework.batch.support.PropertiesConverter;
@@ -67,7 +68,8 @@ class RestartFunctionalTests {
* @throws Exception
*/
@Test
void testLaunchJob() throws Exception {
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE");
JobExecution jobExecution = runJobForRestartTest();

View File

@@ -18,6 +18,8 @@ package org.springframework.batch.sample;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.sample.config.DataSourceConfiguration;
import org.springframework.batch.sample.config.JobRunnerConfiguration;
import org.springframework.batch.sample.config.RetrySampleConfiguration;
@@ -32,6 +34,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
*
* @author Robert Kasanicky
* @author Dave Syer
* @author Mahmoud Ben Hassine
*/
@SpringJUnitConfig(
classes = { DataSourceConfiguration.class, RetrySampleConfiguration.class, JobRunnerConfiguration.class })
@@ -47,8 +50,9 @@ class RetrySampleConfigurationTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testLaunchJob() throws Exception {
jobLauncherTestUtils.launchJob();
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
this.jobLauncherTestUtils.launchJob();
// items processed = items read + 2 exceptions
assertEquals(itemGenerator.getLimit() + 2, itemProcessor.getCounter());
}

View File

@@ -18,6 +18,8 @@ package org.springframework.batch.sample;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.sample.domain.trade.internal.GeneratingTradeItemReader;
import org.springframework.batch.sample.support.RetrySampleItemWriter;
import org.springframework.batch.test.JobLauncherTestUtils;
@@ -28,6 +30,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
* Checks that expected number of items have been processed.
*
* @author Robert Kasanicky
* @author Mahmoud Ben Hassine
*/
@SpringJUnitConfig(
locations = { "/simple-job-launcher-context.xml", "/jobs/retrySample.xml", "/job-runner-context.xml" })
@@ -43,8 +46,9 @@ class RetrySampleFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testLaunchJob() throws Exception {
jobLauncherTestUtils.launchJob();
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
this.jobLauncherTestUtils.launchJob();
// items processed = items read + 2 exceptions
assertEquals(itemGenerator.getLimit() + 2, itemProcessor.getCounter());
}

View File

@@ -20,6 +20,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.scope.context.ChunkContext;
@@ -35,7 +36,8 @@ class TaskletJobFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testLaunchJob() throws Exception {
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
JobExecution jobExecution = jobLauncherTestUtils
.launchJob(new JobParametersBuilder().addString("value", "foo").toJobParameters());
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

View File

@@ -32,6 +32,8 @@ import javax.sql.DataSource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -81,8 +83,9 @@ class TradeJobFunctionalTests {
}
@Test
void testLaunchJob() throws Exception {
jobLauncherTestUtils.launchJob();
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
this.jobLauncherTestUtils.launchJob();
customers = Arrays.asList(new Customer("customer1", (credits.get("customer1") - 98.34)),
new Customer("customer2", (credits.get("customer2") - 18.12 - 12.78)),

View File

@@ -23,6 +23,7 @@ import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
@@ -46,6 +47,7 @@ import org.springframework.test.context.support.DependencyInjectionTestExecution
* to parse the outputs.
*
* @author Robert Kasanicky
* @author Mahmoud Ben Hassine
*/
@SpringJUnitConfig(
locations = { "/simple-job-launcher-context.xml", "/job-runner-context.xml", "/jobs/ioSampleJob.xml" })
@@ -62,7 +64,8 @@ abstract class AbstractIoSampleTests {
* Check the resulting credits correspond to inputs increased by fixed amount.
*/
@Test
void testUpdateCredit() throws Exception {
void testUpdateCredit(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
open(reader);
List<CustomerCredit> inputs = getCredits(reader);

View File

@@ -17,6 +17,8 @@
package org.springframework.batch.sample.iosample;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.test.AssertFile;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -25,6 +27,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
/**
* @author Dan Garrette
* @author Mahmoud Ben Hassine
* @since 2.0
*/
@SpringJUnitConfig(
@@ -42,8 +45,14 @@ class MultiLineFunctionalTests {
* Output should be the same as input
*/
@Test
void testJob() throws Exception {
jobLauncherTestUtils.launchJob();
void testJob(@Autowired Job job) throws Exception {
// given
this.jobLauncherTestUtils.setJob(job);
// when
this.jobLauncherTestUtils.launchJob();
// then
AssertFile.assertFileEquals(new FileSystemResource(INPUT_FILE), new FileSystemResource(OUTPUT_FILE));
}

View File

@@ -17,6 +17,8 @@
package org.springframework.batch.sample.iosample;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.test.AssertFile;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -25,6 +27,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
/**
* @author Dan Garrette
* @author Mahmoud Ben Hassine
* @since 2.0
*/
@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml", "/jobs/iosample/multiRecordType.xml",
@@ -42,8 +45,14 @@ class MultiRecordTypeFunctionalTests {
* Output should be the same as input
*/
@Test
void testJob() throws Exception {
void testJob(@Autowired Job job) throws Exception {
// given
this.jobLauncherTestUtils.setJob(job);
// when
jobLauncherTestUtils.launchJob();
// then
AssertFile.assertFileEquals(new FileSystemResource(INPUT_FILE), new FileSystemResource(OUTPUT_FILE));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2020 the original author or authors.
* Copyright 2006-2022 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.
@@ -86,7 +86,6 @@ public class JobLauncherTestUtils {
* The Job instance that can be manipulated (e.g. launched) in this utility.
* @param job the {@link AbstractJob} to use
*/
@Autowired
public void setJob(Job job) {
this.job = job;
}

View File

@@ -60,9 +60,13 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
* &#064;Autowired
* private JobRepositoryTestUtils jobRepositoryTestUtils;
*
* &#064;Autowired
* private Job jobUnderTest;
*
* &#064;Before
* public void clearJobExecutions() {
* public void setup() {
* this.jobRepositoryTestUtils.removeJobExecutions();
* this.jobLauncherTestUtils.setJob(this.jobUnderTest);
* }
*
* &#064;Test
@@ -86,7 +90,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
*
* <pre class="code">
* &#064;SpringBatchTest
* &#064;ContextConfiguration(classes = MyBatchJobConfiguration.class)
* &#064;SpringJUnitConfig(MyBatchJobConfiguration.class)
* public class MyBatchJobTests {
*
* &#064;Autowired
@@ -101,8 +105,9 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
* }
*
* &#064;Test
* public void testMyJob() throws Exception {
* public void testMyJob(@Autowired Job jobUnderTest) throws Exception {
* // given
* this.jobLauncherTestUtils.setJob(jobUnderTest);
* JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();
*
* // when
@@ -116,13 +121,11 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
* </pre>
*
* <p>
* <strong> It should be noted that {@link JobLauncherTestUtils} requires a
* {@link org.springframework.batch.core.Job} bean and that {@link JobRepositoryTestUtils}
* requires a {@link javax.sql.DataSource} bean. Since this annotation registers a
* {@link JobLauncherTestUtils} and a {@link JobRepositoryTestUtils} in the test context,
* it is expected that the test context contains a single autowire candidate for a
* {@link org.springframework.batch.core.Job} and a {@link javax.sql.DataSource} (either a
* single bean definition or one that is annotated with
* <strong> It should be noted that {@link JobRepositoryTestUtils} requires a
* {@link javax.sql.DataSource} bean. Since this annotation registers a
* {@link JobRepositoryTestUtils} in the test context, it is expected that the test
* context contains a single autowire candidate for a {@link javax.sql.DataSource} (either
* a single bean definition or one that is annotated with
* {@link org.springframework.context.annotation.Primary}). </strong>
* </p>
*

View File

@@ -24,6 +24,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.test.sample.SampleTasklet;
import org.springframework.beans.factory.annotation.Autowired;
@@ -36,6 +37,7 @@ import org.springframework.test.jdbc.JdbcTestUtils;
* This is an abstract test class.
*
* @author Dan Garrette
* @author Mahmoud Ben Hassine
* @since 2.0
*/
@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml", "/job-runner-context.xml" })
@@ -63,7 +65,8 @@ abstract class AbstractSampleJobTests {
}
@Test
void testJob() throws Exception {
void testJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
assertEquals(BatchStatus.COMPLETED, jobLauncherTestUtils.launchJob().getStatus());
this.verifyTasklet(1);
this.verifyTasklet(2);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018-2020 the original author or authors.
* Copyright 2018-2022 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.
@@ -67,6 +67,9 @@ public class SpringBatchTestJUnit4Tests {
@Autowired
private ItemReader<String> jobScopedItemReader;
@Autowired
private Job jobUnderTest;
@Before
public void setUp() {
this.jobRepositoryTestUtils.removeJobExecutions();
@@ -100,6 +103,9 @@ public class SpringBatchTestJUnit4Tests {
@Test
public void testJob() throws Exception {
// given
this.jobLauncherTestUtils.setJob(this.jobUnderTest);
// when
JobExecution jobExecution = this.jobLauncherTestUtils.launchJob();

View File

@@ -88,8 +88,9 @@ public class SpringBatchTestJUnit5Tests {
}
@Test
void testJob() throws Exception {
void testJob(@Autowired Job jobUnderTest) throws Exception {
// given
this.jobLauncherTestUtils.setJob(jobUnderTest);
JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();
// when

View File

@@ -53,9 +53,14 @@ class StepScopeAnnotatedListenerIntegrationTests {
JobLauncherTestUtils jobLauncherTestUtils;
@Test
void test() {
void test(@Autowired Job job) {
// given
this.jobLauncherTestUtils.setJob(job);
// when
JobExecution jobExecution = jobLauncherTestUtils.launchStep("step-under-test");
// then
assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
}

View File

@@ -20,8 +20,10 @@ import io.micrometer.core.tck.MeterRegistryAssert;
import io.micrometer.tracing.test.SampleTestRunner;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.observability.BatchMetrics;
@@ -44,6 +46,11 @@ class ObservabilitySampleStepTests extends SampleTestRunner {
super(SampleRunnerConfig.builder().build(), BatchMetrics.observationRegistry, Metrics.globalRegistry);
}
@BeforeEach
void setup(@Autowired Job job) {
this.jobLauncherTestUtils.setJob(job);
}
@AfterEach
void clean() {
Metrics.globalRegistry.clear();