0

我有一个使用 Quartz 调度程序的 Spring Boot (2.1.3) 项目。它通过启动器包含在内:

dependencies {
    ...
    implementation('org.springframework.boot:spring-boot-starter-quartz')
}

该应用程序(主要)配置有组件扫描。如果我运行应用程序一切都很好。如果我运行一个带有@SprinBootTest所有注释的测试也很好。但是如果我使用这个自定义注释

@DataJpaTest
@ComponentScan(basePackages = ["com.mycompany"])
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ActiveProfiles("intTest")
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@Inherited
annotation class JpaTest

而不是@SpringBootTest我得到一个NoSuchBeanDefinitionException,因为找不到石英Scheduler

我尝试将 Quartz 包添加到组件扫描中,但这无济于事:

@ComponentScan(basePackages = ["com.mycompany", "org.quartz"])

如何使用我的自定义配置注释让 Spring 使用我的测试设置来选择调度程序?

4

2 回答 2

0

我假设您的 SpringBoot 应用程序在@EnableScheduling某处声明。尝试将其添加到您的@JpaTest

于 2019-04-25T14:21:43.733 回答
0

我不知道如何修复组件扫描本身,但一种解决方法是Scheduler在代码中显式注册:

@Configuration
class SchedulerConfiguration {

    @Bean
    fun scheduler(): Scheduler = StdSchedulerFactory.getDefaultScheduler()
}
于 2019-04-26T07:04:40.523 回答