我的src/test/java
:中有以下Spring Batch测试
@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() {
...
}
我定义我的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
在我的主要应用程序中,SpringBatch配置:
@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);
}
}
当我尝试运行集成测试时,我的应用程序-local-test.yml没有被选中:
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)
我做错了什么?
在使用@SpringBatchTest
编写集成测试时,我遇到了无法加载application.yaml的相同情况。
我最终添加了@SpringBootTest
来解决这个问题。
根据文档,@SpringBatchTest注册了JobLauncherTestUtils、JobRepositoryTestUtils等Bean。。。等进行测试。
然而,如果我们想启用春季启动功能,如加载应用程序属性,添加@SpringBootTest似乎是一种方法。(文档(