Mockito Spring Camel @Autowire fail - PullRequest
       7

Mockito Spring Camel @Autowire fail

0 голосов
/ 04 июля 2018

Я пытаюсь протестировать верблюжий маршрут. Тестируемый маршрут расширяет пользовательский абстрактный RouteBuilder (я знаю о предпочтении композиции перед наследованием - это код обслуживания). Я настроил свой тест, как @Roman Vottner здесь . Все работает (инициализируется), пока я не достиг первого абстрактного класса по иерархии. У него есть класс @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!
//...

1 Ответ

0 голосов
/ 04 июля 2018

Решил это, явно объявив 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;
}

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...