我已将
@SchedulerLock
添加到现有 @Scheduled
任务中。现在我的集成测试失败了。我为 SchedulerLock 添加了以下依赖项:
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-spring</artifactId>
<version>5.11.0</version>
</dependency>
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-provider-jdbc-template</artifactId>
<version>5.11.0</version>
</dependency>
在我的
ScheduleService
中,我有多个需要使用的自动连线服务。例如。像这样:
SomeService someService;
@Scheduled(cron = "0 0 3 * * ?")
@SchedulerLock(name = "StatisticService_processStatisticsAtThreeAM", lockAtLeastFor = "PT10M")
void methodToSchedule() {
someService.method();
}
运行我之前工作的集成测试现在会导致异常:
Caused by: java.lang.NullPointerException: Cannot invoke "package.to.service.SomeService .method()" because "this.someService" is null
我的集成测试设置如下:
@ActiveProfiles("test")
@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc
@WithMockUser
@Transactional
public class MyTest {
@Autowired ScheduleService service;
/* tests */
}
PS:应用程序本身正在运行,并且锁已正确放入我的数据库中。
我通过调用另一个服务的方法修复了它:
public class ScheduleService {
SomeService someService;
// originally called with Scheduling
void methodToSchedule() {
someService.method();
}
}
public class Schedulerr {
ScheduleService service;
void newScheduleMethod() {
service.methodToSchedule();
}
}