Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

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;
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
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;
Expand Down Expand Up @@ -62,8 +61,7 @@ public void setDataSource(DataSource dataSource) {
}

@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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
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;
Expand Down Expand Up @@ -82,8 +81,7 @@ void tearDown() {
}

@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"))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

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;
Expand All @@ -43,8 +42,7 @@ public void setDataSource(DataSource dataSource) {
}

@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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
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;
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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;
Expand All @@ -43,8 +42,7 @@ void removeOldData() throws IOException {
}

@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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

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;
Expand All @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

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;
Expand Down Expand Up @@ -92,8 +91,7 @@ public void setDataSource(DataSource dataSource) {
}

@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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
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;
Expand Down Expand Up @@ -96,8 +95,7 @@ void after() {
}

@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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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;
Expand All @@ -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"), ""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
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;
Expand All @@ -46,8 +45,7 @@ public void setDataSource(DataSource dataSource) {
}

@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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
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;
Expand Down Expand Up @@ -64,8 +63,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
* 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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
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;
Expand Down Expand Up @@ -64,8 +63,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
* 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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@

package org.springframework.batch.sample;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;

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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
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;
Expand Down Expand Up @@ -70,8 +69,7 @@ void onTearDown() {
* @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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

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;
Expand Down Expand Up @@ -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());
Expand Down
Loading