Reputation: 529
Read a few blogs on testing spring batch and set up a test accordingly (see below). The test completes. However, I have two questions:
JdbcTemplate
or implement a query possibility on the server e.g. via rest (which then would be productive code). Is that correct? Are there other possibilities?Testcode:
@SpringBootTest
@RunWith(SpringRunner.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class FooJobTest {
@Inject
private Job fooBatchJob;
@Inject
private JobLauncher jobLauncher;
private JobLauncherTestUtils jobLauncherTestUtils;
@Before
public void setUp() {
jobLauncherTestUtils = new JobLauncherTestUtils();
jobLauncherTestUtils.setJob(fooBatchJob);
jobLauncherTestUtils.setJobLauncher(jobLauncher);
}
@Test
public void testFooJob() {
final JobExecution jobExecution = jobLauncherTestUtils.launchJob();
assertThat(jobExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED);
// would like to query the database
}
}
Upvotes: 2
Views: 226
Reputation: 21463
To answer your questions:
JobLauncher
uses the TaskExecutor
you have configured which, by default, is a synchronous one. That being said, if you are using any of the asynchronous TaskExecutor
implementations, you'd have to poll for the results (or add a JobExecutionListener
to alert that it is complete).JdbcTemplate
to query and validate your data. Adding REST APIs to your server application just for use in test probably is a bad idea IMHO.Upvotes: 2