'How do I get a SpringBatch Integration test to load my application properties?
I have the following Spring Batch test in my src/test/java
:
@RunWith(SpringRunner.class)
@SpringBatchTest
@EnableAutoConfiguration
@ContextConfiguration(classes= MyBatchConfig.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class})
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
@ActiveProfiles("local-test")
public class MyIntegrationTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Autowired
private JobRepositoryTestUtils jobRepositoryTestUtils;
@Test
public void testJob() {
...
}
I define my test/resources/application-local-test.yml
:
spring:
profiles:
active: "test"
datasource:
driverClassName: org.h2.Driver
url: jdbc:h2:mem:TEST_DB;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;IGNORECASE=TRUE;
username: sa
password: pwd
jpa:
open-in-view: true
show-sql: true
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
ddl-auto: create
h2:
console: enabled
cloud:
aws:
s3:
bucket: testBucket
and in my main application, a SpringBatch Configuration:
@Configuration
@EnableBatchProcessing
public class ImportProjectsBatchConfig {
public static final String JOB_NAME = "myJob";
private String s3BucketName;
public ImportProjectsBatchConfig(
@Value("${cloud.aws.s3.bucket}")
String s3BucketName) {
this.s3BucketName = s3BucketName;
}
@Bean
public String s3BucketName() {
return s3BucketName;
}
@Bean
public SimpleStepBuilder<WebProject, WebProject> simpleStepBuilder(StepBuilderFactory stepBuilderFactory,
ItemProcessor itemProcessor, ItemWriter itemWriter,
MyErrorItemListener errorItemListenerSupport) {
return stepBuilderFactory.get(JOB_NAME).<WebProject, WebProject>chunk(chunkSize)
.processor(itemProcessor).listener((ItemProcessListener) errorItemListenerSupport)
.writer(itemWriter).listener((ItemWriteListener) errorItemListenerSupport);
}
}
When I try to run the integration test my application-local-test.yml is not getting picked up:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'cloud.aws.s3.bucket' in value "${cloud.aws.s3.bucket}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:178)
What am I doing wrong?
Solution 1:[1]
I ran into the same situation that I cannot load the application.yaml while using @SpringBatchTest
to write an integration test.
I ended up adding @SpringBootTest
to solve the problem.
According to the document, @SpringBatchTest register beans like JobLauncherTestUtils, JobRepositoryTestUtils...etc for the test.
However if we want to enable spring boot functionalities like loading application properties, adding @SpringBootTest seems to be a way. (document)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Greg |