我正在嘗試對駱駝路線進行單元測試。被測試的路由擴展了一個自定義的抽象RouteBuilder(我知道關于繼承優先于繼承-這是維護代碼)。我已經像@Roman Vottner在這里所做的那樣設置了測試。一切正常(初始化),直到我到達層次結構中的第一個抽象類為止。它具有一個@Autowired類,即使在測試開始時對其進行了模擬和@Autowired,該類也未初始化(為null)。關于如何解決注射問題的任何想法?@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));//... }}在調試時在抽象超類中:@Autowiredpublic ClientServices clientServices;//...String clientNumber=clientServices.getLoggedInNumber(); //clientServices is null and not mocked!//...
1 回答
楊__羊羊
TA貢獻1943條經驗 獲得超7個贊
通過將FooRoute顯式聲明為Bean來解決此問題:
@Bean
public FooRoute fooRoute(){
return new FooRoute();
}
@Override
public List<RouteBuilder> routes() {
final List<RouteBuilder> routes = new ArrayList<>();
routes.add(fooRoute());
return routes;
}

添加回答
舉報
0/150
提交
取消
