Reputation: 339
Trying to create some end-to-end tests for an spring batch application, which works great. I get an sql error because it is not initializing Spring Batch processing tables: org.postgresql.util.PSQLException: ERROR: relation "batch_job_instance" does not exist
I have this code in the src/test/resources/application.properties
:
spring.datasource.initialize=true
spring.datasource.initialization-mode=always
spring.datasource.platform=postgresql
spring.batch.initialize-schema=always
Which is the same I have on `src/main/resources/application.properties and works.
This is the code I have for ApplicationTest
:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes={
TestConfiguration.class,
JobCompletionNotificationListener.class,
BatchConfiguration.class
})
@SpringBatchTest
public class ApplicationTests {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
public void testJob() throws Exception {
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
}
}
I have an specific TestConfiguration
to generate a Bean
with the DataSource
.
@Configuration
@PropertySource("application.properties")
public class TestConfiguration {
@Autowired
private Environment env;
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("spring.datasource.driverClassname"));
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));
dataSource.setPassword(env.getProperty("spring.datasource.password"));
return dataSource;
}
I was expecting all tables to be created (internal Batch tables and the tables defined in schema-all.sql
).
But I get the following error org.postgresql.util.PSQLException: ERROR: relation "batch_job_instance" does not exist
.
I don't understand why in the main application all works automagically, and it doesn't in the test.
Upvotes: 5
Views: 1995
Reputation: 3889
If a Spring test misses misses the BatchDataSourceInitializer
that is being auto-configured by Spring Boot in the actual application, and you don't want to write a full @SpringBootTest
, you can selectively add the auto-configuration for Spring Batch by adding the annotation
@ImportAutoConfiguration(BatchAutoConfiguration.class)
This will then provide the initializer for the injected DataSource
.
Upvotes: 1