gpt4 book ai didi

java - 如何在 Spring Boot 应用程序中为 @Scheduled cron 作业编写集成测试?

转载 作者:行者123 更新时间:2023-12-04 07:19:20 27 4
gpt4 key购买 nike

我有一个 cronjob 应用程序类
过期OIrderJob.java

@Service
@AllArgsConstructor
public class ExpireOrdersJob {
private final ExpireOrderProperties expireOrderProperties;
private final ExpireOrderUseCase expireOrderUseCase;

@Scheduled(cron = "${worker.cron.expire-order.cron-time-pattern}")
public void process() {
expireOrderUseCase.expire(expireOrderProperties.getExpireTimeInMinutes());
}
}
我为上述类创建了一个集成测试,例如
ExpireOrderJobIT.java
@RunWith(SpringRunner.class)
@SpringBootTest
@SpringJUnitConfig(SchedulingConfig.class)
@ContextConfiguration(classes = {ExpireOrderConfig.class})
@TestPropertySource(properties = "worker.cron.expire-order.cron-time-pattern=* * * * * *")
public class ExpireOrderJobIT {
@Autowired
private ExpireOrdersJob expireOrdersJob;

@SneakyThrows
@Test
public void scheduleRunExpireJob() {
Thread.sleep(1000L);
expireOrdersJob.process();
}
}
当我在生产环境中运行应用程序时,我希望我的测试可以覆盖所有的类和函数关系,所以我不会使用 Mockito @SpyBean,例如 https://www.baeldung.com/spring-testing-scheduled-annotation .
它不能覆盖我的服务,用例......请帮忙
我的测试 throw 错误
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'net.vinid.ecom.order.worker.cron.ExpireOrdersJob' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1790)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1346)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657)
... 30 more

最佳答案

你的单元测试很困惑,你有这三个注释:

@RunWith(SpringRunner.class)
@SpringBootTest
@SpringJUnitConfig(SchedulingConfig.class)
它们之间不兼容,因为它们几乎相同,只有一个就足够了。这个其他注释 @ContextConfiguration(classes = {ExpireOrderConfig.class})指定要使用的spring上下文,目前你只有 ExpireOrderConfig类,并根据您的错误消息看起来像 ExpireOrdersJob没有在那里初始化。我不知道您的完整应用程序上下文,因此我会建议一个暂定的解决方案,尽管我可能缺少上下文中的类。
@SpringBootTest
@ContextConfiguration(classes = {ExpireOrdersJob.class, ExpireOrderUseCase.class, ExpireOrderProperties.class, SchedulingConfig.class, ExpireOrderConfig.class})
@TestPropertySource(properties = "cron.rate=1000") //How often you expect your job to run, you can place your cron here.
class ExpireOrdersJobIntegrationTest {

@Autowired
ExpireOrdersJob expireOrdersJob;
@Autowired
ExpireOrderUseCase expireOrderUseCase;

@Test
void givenExpirationInMinutes_thenExpireOrderUseCaseIsCalledOnceIn1Second() {
//Based on the rate I set, I expect it to be called in 1s at most.
await()
.atMost(Duration.ONE_SECOND)
.untilAsserted(() -> assertThat(expireOrderUseCase.getExpireCount().get()).isEqualTo(1L));
}
}
我在 @ContextConfiguration 中添加了我认为您需要用于此测试的类,但是可能缺少某些类或某些类是不必要的(例如,如果在 ExpireOrderConfig 中初始化了另一个 bean)。
此外,正如您所看到的,我的断言正在检查 getExpireCount() , 我在方法 expire 中添加了这个计数器只是为了查看我的方法被调用了多少次,但是考虑到您正在编写集成测试,您应该替换此断言以替换您希望验证的副作用(例如数据库更改、状态更改、休息调用,取决于你的逻辑。)

关于java - 如何在 Spring Boot 应用程序中为 @Scheduled cron 作业编写集成测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68601870/

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