我正在嘗試對(duì)駱駝路線進(jìn)行單元測(cè)試。被測(cè)試的路由擴(kuò)展了一個(gè)自定義的抽象RouteBuilder(我知道關(guān)于繼承優(yōu)先于繼承-這是維護(hù)代碼)。我已經(jīng)像@Roman Vottner在這里所做的那樣設(shè)置了測(cè)試。一切正常(初始化),直到我到達(dá)層次結(jié)構(gòu)中的第一個(gè)抽象類為止。它具有一個(gè)@Autowired類,即使在測(cè)試開始時(shí)對(duì)其進(jìn)行了模擬和@Autowired,該類也未初始化(為null)。關(guān)于如何解決注射問題的任何想法?@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));//... }}在調(diào)試時(shí)在抽象超類中:@Autowiredpublic ClientServices clientServices;//...String clientNumber=clientServices.getLoggedInNumber(); //clientServices is null and not mocked!//...
1 回答

楊__羊羊
TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊
通過將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;
}
添加回答
舉報(bào)
0/150
提交
取消