Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot Spring:在测试类中注册组件_Spring Boot_Spring Test - Fatal编程技术网

Spring boot Spring:在测试类中注册组件

Spring 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

我正在为我的Spring
调度程序注册一个
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

我注意到的一件重要事情是内部类必须是
静态的