Spring boot Spring:在测试类中注册组件
我正在为我的SpringSpring boot Spring:在测试类中注册组件,spring-boot,spring-test,Spring Boot,Spring Test,我正在为我的Spring调度程序注册一个ErrorHandler,并想测试它是否在SpringTest中正确注册 到目前为止,我已经尝试: 处理程序 @Component public class ScheduledErrorHandler implements ErrorHandler { @Autowired private ErrorService errorService; @Override public void handleError(final
调度程序注册一个ErrorHandler
,并想测试它是否在SpringTest中正确注册
到目前为止,我已经尝试:
处理程序
@Component
public class ScheduledErrorHandler implements ErrorHandler {
@Autowired
private ErrorService errorService;
@Override
public void handleError(final Throwable t) {
errorService.handle(t);
}
}
@EnableScheduling
@Configuration
public class SchedulingConfiguration implements SchedulingConfigurer {
@Autowired
private ScheduledErrorHandler handler;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
final ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(1);
scheduler.setErrorHandler(handler);
scheduler.initialize();
taskRegistrar.setScheduler(scheduler);
}
//...
}
注册处理程序
@Component
public class ScheduledErrorHandler implements ErrorHandler {
@Autowired
private ErrorService errorService;
@Override
public void handleError(final Throwable t) {
errorService.handle(t);
}
}
@EnableScheduling
@Configuration
public class SchedulingConfiguration implements SchedulingConfigurer {
@Autowired
private ScheduledErrorHandler handler;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
final ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(1);
scheduler.setErrorHandler(handler);
scheduler.initialize();
taskRegistrar.setScheduler(scheduler);
}
//...
}
测试它是否已注册
@ContextConfiguration(classes = {
SchedulerConfiguration.class,
SchedulerErrorHandler.class
})
@RunWith(SpringRunner.class)
public class SchedulerErrorHandlerTest {
@MockBean
private ErrorService service;
@Autowired
private ExampleScheduledJob job;
@Test
public void verifyHandlerGetsCalled() {
// Wait until the job runs
if(!job.latch.await(5, SECONDS)) {
fail("Job never ran");
}
verify(service).handle(any(RuntimeException.class));
}
@Component
public static class ExampleScheduledJob {
private final CountDownLatch latch = new CountDownLatch(1);
@Scheduled(fixedRate=1000)
public void run() {
latch.countDown();
throw new RuntimeException("error");
}
}
}
然而,当我这样做时,我得到了一个DependencyNotFound
错误,说Spring无法创建我的测试类,因为找不到名为ExampleScheduledJob
的Bean。我如何才能仅为本次测试注册它
创建名为的bean时出错
'com.example.demo.SchedulerErrorHandlerTest':未满足的依赖项
通过字段“作业”表示;嵌套异常是
org.springframework.beans.factory.noSuchBean定义异常:否
类型的限定bean
'com.example.demo.SchedulerErrorHandlerTest$ExampleScheduledJob'
可用:至少需要1个符合autowire条件的bean
候选人依赖项批注:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
这应该行得通
@ContextConfiguration(classes = {
SchedulingConfiguration.class,
SchedulerErrorHandlerTest.ExampleScheduledJob.class,
ScheduledErrorHandler.class
})
@RunWith(SpringRunner.class)
您可以如上所述注册测试配置类(ExampleScheduledJob)。因为它是一个静态的内部类,所以需要像使用SchedulerErrorHandlerTest一样使用它。例如scheduledjob
我注意到的一件重要事情是内部类必须是静态的