【发布时间】:2018-07-03 21:39:08
【问题描述】:
我正在尝试对骆驼路线进行单元测试。被测路由扩展了一个自定义抽象 RouteBuilder(我知道有利于组合而不是继承 - 这是维护代码)。我已经按照@Roman Vottner 所做的over here 设置了我的测试。一切正常(已初始化),直到我点击层次结构中的第一个抽象类。它有一个未初始化的@Autowired 类(为空),即使它在测试开始时被模拟和@Autowired。关于如何解决我的注射问题的任何想法?
@RunWith(CamelSpringRunner.class)
@BootstrapWith(CamelTestContextBootstrapper.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = {FooRouteTest.ContextConfig.class})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class FooRouteTest {
@Configuration
@PropertySource({"classpath:some.properties", "classpath:environment.properties"})
public static class ContextConfig extends CamelConfiguration {
@Bean
public UserServices userServices() {
return mock(UserServices.class);
} //and many more of the like
}
@Autowired
private UserServices userServices; //and all the others too
@Test
public void testAfoo() throws Exception {
//....
template.setDefaultEndpointUri("direct://getTheData");
template.sendBody(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode));
//...
}
}
在抽象超类中调试:
@Autowired
public ClientServices clientServices;
//...
String clientNumber=clientServices.getLoggedInNumber(); //clientServices is null and not mocked!
//...
【问题讨论】:
标签: java spring apache-camel mockito