Java springboot:将模拟Bean插入Camel处理器单元测试
我在Spring Boot应用程序中有一个骆驼处理器,我正试图为它编写单元测试。processor类如下所示:Java springboot:将模拟Bean插入Camel处理器单元测试,java,spring-boot,apache-camel,Java,Spring Boot,Apache Camel,我在Spring Boot应用程序中有一个骆驼处理器,我正试图为它编写单元测试。processor类如下所示: @Named public class MyProcessor implements Processor { @Inject private MyService myService; @Override public void process(Exchange exchange) { ... } } 我试图将一个模拟MyServ
@Named
public class MyProcessor implements Processor {
@Inject
private MyService myService;
@Override
public void process(Exchange exchange) {
...
}
}
我试图将一个模拟MyService对象注入到单元测试中
我试过这个:
@RunWith(CamelSpringRunner.class)
@SpringBootTest
public class MyProcessorTest {
@TestConfiguration
static class Config {
@Autowired
private MyProcessor myProcessor;
@Bean
CamelContextConfiguration myProcessorTestContextConfiguration() {
return new CamelContextConfiguration() {
@Override
public void beforeApplicationStart(CamelContext camelContext) {
MyService myService = Mockito.mock(MyService.class);
camelContext.addService(myService);
...
。。。但这似乎不起作用——一个非模拟的MyService实例被注入处理器。我认为问题是因为我的模拟服务对象是在Camel上下文中创建的,而不是通过Spring创建的
有人能告诉我正确的方法吗
提前感谢您的帮助。您可以使用
@MockBean
@SpringBootTest
...
@MockBean
private MyService myService;
这将在Spring测试上下文中创建类型为
MyService
的Mockito Mock,并(因此)将其注入到通常会注入真实Bean的位置。耶@inject
和@Named
是Spring注释,它的Spring创建并向处理器中注入依赖项。因此,寻找如何用spring模拟这个问题,您就有机会找到解决方案