Improve @SpringBatchTest to autowire the job under test in JobLauncherTestUtils if it is unique

Issue #4218
This commit is contained in:
Henning Poettker
2022-10-23 22:15:44 +02:00
committed by Mahmoud Ben Hassine
parent 621ec92586
commit 3bb7ce532b
34 changed files with 201 additions and 102 deletions

View File

@@ -17,7 +17,6 @@ package org.springframework.batch.sample;
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;
@@ -50,9 +49,8 @@ class AMQPJobFunctionalTests {
private JobExplorer jobExplorer;
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
void testLaunchJob() throws Exception {
// given
this.jobLauncherTestUtils.setJob(job);
this.jobLauncherTestUtils.launchJob();
// when

View File

@@ -18,7 +18,6 @@ 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;
@@ -31,10 +30,7 @@ class BeanWrapperMapperSampleJobFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testJobLaunch(@Autowired Job job) throws Exception {
// given
this.jobLauncherTestUtils.setJob(job);
void testJobLaunch() throws Exception {
// when
this.jobLauncherTestUtils.launchJob();

View File

@@ -28,7 +28,6 @@ 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;
@@ -62,8 +61,7 @@ class CompositeItemWriterSampleFunctionalTests {
}
@Test
void testJobLaunch(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testJobLaunch() throws Exception {
JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE");
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE");

View File

@@ -29,7 +29,6 @@ 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;
@@ -82,8 +81,7 @@ class CustomerFilterJobFunctionalTests {
}
@Test
void testFilterJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testFilterJob() throws Exception {
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
customers = Arrays.asList(new Customer("customer1", (credits.get("customer1"))),

View File

@@ -21,7 +21,6 @@ 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;
@@ -57,8 +56,7 @@ class DatabaseShutdownFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
Thread.sleep(1000);

View File

@@ -17,7 +17,6 @@ package org.springframework.batch.sample;
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;
@@ -37,8 +36,7 @@ class DelegatingJobFunctionalTests {
private PersonService personService;
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
jobLauncherTestUtils.launchJob();
assertTrue(personService.getReturnedCount() > 0);

View File

@@ -19,7 +19,6 @@ 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;
@@ -43,8 +42,7 @@ class FootballJobFunctionalTests {
}
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
JdbcTestUtils.deleteFromTables(jdbcTemplate, "PLAYERS", "GAMES", "PLAYER_SUMMARY");
jobLauncherTestUtils.launchJob();

View File

@@ -21,7 +21,6 @@ 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;
@@ -58,8 +57,7 @@ class GracefulShutdownFunctionalTests {
private JobOperator jobOperator;
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
final JobParameters jobParameters = new JobParametersBuilder().addLong("timestamp", System.currentTimeMillis())
.toJobParameters();

View File

@@ -23,7 +23,6 @@ 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;
@@ -43,8 +42,7 @@ public class GroovyJobFunctionalTests {
}
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
assertFalse(new File("target/groovyJob/output/files.zip").exists());
jobLauncherTestUtils.launchJob();
assertTrue(new File("target/groovyJob/output/files.zip").exists());

View File

@@ -20,7 +20,6 @@ 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;
@@ -45,8 +44,7 @@ class HeaderFooterSampleFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testJob() throws Exception {
this.jobLauncherTestUtils.launchJob();
BufferedReader inputReader = new BufferedReader(new FileReader(input.getFile()));

View File

@@ -25,7 +25,6 @@ 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;
@@ -92,8 +91,7 @@ class HibernateFailureJobFunctionalTests {
}
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
validatePreConditions();
JobParameters params = new JobParametersBuilder().addString("key", "failureJob").toJobParameters();

View File

@@ -17,7 +17,6 @@ package org.springframework.batch.sample;
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;
@@ -45,8 +44,7 @@ class LoopFlowSampleFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testJobLaunch(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testJobLaunch() throws Exception {
this.jobLauncherTestUtils.launchJob();
// items processed = items read + 2 exceptions
assertEquals(10, itemWriter.getItems().size());

View File

@@ -25,7 +25,6 @@ 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;
@@ -96,8 +95,7 @@ class MailJobFunctionalTests {
}
@Test
void testSkip(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testSkip() throws Exception {
this.createUsers(new Object[][] { USER1, USER2_SKIP, USER3, USER4_SKIP, USER5, USER6, USER7, USER8 });
JobExecution jobExecution = jobLauncherTestUtils.launchJob();

View File

@@ -19,7 +19,6 @@ 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;
@@ -44,8 +43,7 @@ class MultilineJobFunctionalTests {
private final Resource output = new FileSystemResource("target/test-outputs/20070122.testStream.multilineStep.txt");
@Test
void testJobLaunch(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testJobLaunch() throws Exception {
this.jobLauncherTestUtils.launchJob();
assertEquals(EXPECTED_RESULT, StringUtils.replace(IOUtils.toString(output.getInputStream(), "UTF-8"),
System.getProperty("line.separator"), ""));

View File

@@ -22,7 +22,6 @@ import java.nio.file.Path;
import org.junit.jupiter.api.Assertions;
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;
@@ -41,8 +40,7 @@ class MultilineOrderJobFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testJobLaunch(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testJobLaunch() throws Exception {
this.jobLauncherTestUtils.launchJob();
Path expectedFile = new ClassPathResource(EXPECTED).getFile().toPath();
Path actualFile = new FileSystemResource(ACTUAL).getFile().toPath();

View File

@@ -21,7 +21,6 @@ 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.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -46,8 +45,7 @@ class ParallelJobFunctionalTests {
}
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STAGING");
JobExecution execution = jobLauncherTestUtils.launchJob();
int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STAGING");

View File

@@ -24,7 +24,6 @@ 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;
@@ -64,8 +63,7 @@ class PartitionFileJobFunctionalTests implements ApplicationContextAware {
* Check the resulting credits correspond to inputs increased by fixed amount.
*/
@Test
void testUpdateCredit(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testUpdateCredit() throws Exception {
assertTrue(applicationContext.containsBeanDefinition("outputTestReader"),
"Define a prototype bean called 'outputTestReader' to check the output");

View File

@@ -24,7 +24,6 @@ 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;
@@ -64,8 +63,7 @@ class PartitionJdbcJobFunctionalTests implements ApplicationContextAware {
* Check the resulting credits correspond to inputs increased by fixed amount.
*/
@Test
void testUpdateCredit(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testUpdateCredit() throws Exception {
assertTrue(applicationContext.containsBeanDefinition("outputTestReader"),
"Define a prototype bean called 'outputTestReader' to check the output");

View File

@@ -16,7 +16,6 @@
package org.springframework.batch.sample;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -24,7 +23,6 @@ import org.junit.jupiter.api.Assertions;
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;
@@ -54,8 +52,7 @@ class RestartFileSampleFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void runTest(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void runTest() throws Exception {
JobParameters jobParameters = jobLauncherTestUtils.getUniqueJobParameters();
JobExecution je1 = jobLauncherTestUtils.launchJob(jobParameters);

View File

@@ -21,7 +21,6 @@ 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;
@@ -70,8 +69,7 @@ class RestartFunctionalTests {
* @throws Exception
*/
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE");
JobExecution jobExecution = runJobForRestartTest();

View File

@@ -17,7 +17,6 @@ package org.springframework.batch.sample;
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;
@@ -48,8 +47,7 @@ class RetrySampleFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
this.jobLauncherTestUtils.launchJob();
// items processed = items read + 2 exceptions
assertEquals(itemGenerator.getLimit() + 2, itemProcessor.getCounter());

View File

@@ -19,7 +19,6 @@ package org.springframework.batch.sample;
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;
@@ -37,8 +36,7 @@ class TaskletJobFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
JobExecution jobExecution = jobLauncherTestUtils
.launchJob(new JobParametersBuilder().addString("value", "foo").toJobParameters());
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

View File

@@ -30,7 +30,6 @@ 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;
@@ -82,8 +81,7 @@ class TradeJobFunctionalTests {
}
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
this.jobLauncherTestUtils.launchJob();
customers = Arrays.asList(new Customer("customer1", (credits.get("customer1") - 98.34)),

View File

@@ -21,7 +21,6 @@ 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;
@@ -65,9 +64,7 @@ abstract class AbstractIoSampleTests {
* Check the resulting credits correspond to inputs increased by fixed amount.
*/
@Test
void testUpdateCredit(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testUpdateCredit() throws Exception {
open(reader);
List<CustomerCredit> inputs = getCredits(reader);
close(reader);

View File

@@ -22,7 +22,6 @@ import java.nio.file.Path;
import org.junit.jupiter.api.Assertions;
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;
@@ -49,10 +48,7 @@ class MultiLineFunctionalTests {
* Output should be the same as input
*/
@Test
void testJob(@Autowired Job job) throws Exception {
// given
this.jobLauncherTestUtils.setJob(job);
void testJob() throws Exception {
// when
this.jobLauncherTestUtils.launchJob();

View File

@@ -22,7 +22,6 @@ import java.nio.file.Path;
import org.junit.jupiter.api.Assertions;
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;
@@ -49,10 +48,7 @@ class MultiRecordTypeFunctionalTests {
* Output should be the same as input
*/
@Test
void testJob(@Autowired Job job) throws Exception {
// given
this.jobLauncherTestUtils.setJob(job);
void testJob() throws Exception {
// when
jobLauncherTestUtils.launchJob();

View File

@@ -7,4 +7,5 @@
<context:annotation-config/>
<bean class="org.springframework.batch.test.JobLauncherTestUtils"/>
<bean class="org.springframework.batch.test.context.BatchTestContextBeanPostProcessor"/>
</beans>