代码之家  ›  专栏  ›  技术社区  ›  coderatchet

Spring批处理JUnit测试未加载应用程序。JobLauncherTestUtils的属性

  •  1
  • coderatchet  · 技术社区  · 8 年前

    JdbcJobInstanceDao 尝试调用 FIND_JOBS_WITH_KEY 查询:

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

    这个 %PREFIX% 令牌被替换为的值 application.properties spring.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.
            }
        }
    }
    

    :

    spring.batch.table-prefix=MY_SCHEMA_USER.BATCH_
    

    JobLauncherTestUtils 似乎不尊重这些配置属性。

    我需要不同的表前缀,因为批处理数据库表属于连接的数据库用户的不同模式。(即。 MY_APP_USER MY_SCHEMA_USER.BATCH_JOB_INSTANCE ). 对表的非限定引用尝试(但失败)根据 MY_APP_用户 而不是 MY_SCHEMA_USER .

    JobRepositoryFactoryBean bean并用 @ConfigurationProperties("spring.batch") . 然而,无论如何,这都不起作用,我不明白为什么我应该用这种方式而不是用属性来配置它们。

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

    1 回复  |  直到 8 年前
        1
  •  0
  •   coderatchet    8 年前

    固定的

    BatchConfigurer MyBatchConfig

    这意味着没有通过 @Component 注释 DefaultBatchConfigurer 班至于为什么我决定把这段代码放在这里,即使是我——作者——也不确定。我想我不需要在最少的睡眠后编码。

    谢谢你纵容我的愚蠢!