gpt4 book ai didi

java - 如何获取 Spring Batch Integration 测试来加载我的应用程序属性?

转载 作者:行者123 更新时间:2023-12-05 06:19:30 26 4
gpt4 key购买 nike

我的 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);
}

}

当我尝试运行集成测试时,我的 application-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 来解决问题。

根据document , @SpringBatchTest 为测试注册像 JobLauncherTestUtils, JobRepositoryTestUtils...等 bean。

但是,如果我们想要启用 spring boot 功能,例如加载应用程序属性,添加 @SpringBootTest 似乎是一种方法。 ( document )

关于java - 如何获取 Spring Batch Integration 测试来加载我的应用程序属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60822084/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com