Mockito弹簧骆驼@Autowire失败

我正在尝试对骆驼路线进行单元测试。被测试的路由扩展了一个自定义的抽象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));

//...

  }

}

在调试时在抽象超类中:


@Autowired

public ClientServices clientServices;

//...

String clientNumber=clientServices.getLoggedInNumber();  //clientServices is null and not mocked!

//...


慕妹3146593
浏览 191回答 1
1回答

杨__羊羊

通过将FooRoute显式声明为Bean来解决此问题:@Beanpublic FooRoute fooRoute(){&nbsp; return new FooRoute();}@Overridepublic List<RouteBuilder> routes() {&nbsp; final List<RouteBuilder> routes = new ArrayList<>();&nbsp; routes.add(fooRoute());&nbsp; return routes;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java