gpt4 book ai didi

java - Spring Batch JUnit 测试未加载 JobLauncherTestUtils 的 application.properties

转载 作者:太空宇宙 更新时间:2023-11-04 11:06:56 26 4
gpt4 key购买 nike

JdbcJobInstanceDao尝试调用FIND_JOBS_WITH_KEY查询时,问题就出现了:

SELECT JOB_INSTANCE_ID, JOB_NAME from %PREFIX%JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?

%PREFIX% token 被 application.propertiesspring.batch.table-prefix 的值替换,默认为 “BATCH_”

正如我的小测试所示,应用程序属性肯定是从文件加载的:

@ActiveProfiles("test") // to load `application-test.properties`
@RunWith(SpringRunner.class)
// we don't need a web context as we are playing with only server side classes
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = {TestDatabaseConfig.class,
MyBatchProperties.class, SpringBatchTestConfig.class})
@ComponentScan(basePackageClasses = {MyBatchConfig.class})
// MyBatchConfig has @EnableBatchProcessing and all job configurations.
public class BatchTest {

@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;

@Autowired
private ApplicationContext applicationContext;

@Before
public void setup() {
Environment environment = applicationContext.getEnvironment();
System.out.println(environment.getProperty("spring.batch.table-prefix"));
// above prints MY_SCEHMA_USER.BATCH_ as expected
}

@Test
public void checkJobRuns() {
try {
jobLauncherTestUtils.launchJob();
} catch (Exception e) {
e.printStackTrace(); // <-- fails here with the query returning "table not found" because the prefix was not configured correctly.
}
}
}

application-test.properties:

spring.batch.table-prefix=MY_SCHEMA_USER.BATCH_

我长期以来一直在使用作业运行的自定义配置,但 JobLauncherTestUtils 似乎并不支持这些配置属性。

I need the different table prefix as the batch database tables are owned by a different schema to the connected database user. (i.e. MY_APP_USER trying to access MY_SCHEMA_USER.BATCH_JOB_INSTANCE). unqualified references to tables try (and fail) to resolve the batch tables against MY_APP_USER instead of MY_SCHEMA_USER.

我尝试创建一个 JobRepositoryFactoryBean bean 并使用 @ConfigurationProperties("spring.batch") 对其进行注释。但是 - 而且无论如何都不起作用 - 我不明白为什么我应该以这种方式配置这些而不是使用属性。

如何使用 JobLauncherTestUtils 在 junit 测试中使用应用程序属性正确配置与 Batch 相关的 Bean?

最佳答案

已修复

问题是因为我在 MyBatchConfig 类中创建了自己的 BatchConfigurer bean。

The BatchConfigurer registers the properties for configuration with each component of the Batch framework (jobRepository, daos, etc...)

这意味着属性不是通过带有 @Component 注释的 DefaultBatchConfigurer 类填充的。至于为什么我决定将这段代码放在这里,即使我 - 作者 - 也不确定。我想我不需要在最少的 sleep 后编码。

谢谢你纵容我的愚蠢!

关于java - Spring Batch JUnit 测试未加载 JobLauncherTestUtils 的 application.properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46396538/

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