简体   繁体   English

Apache骆驼Junit模拟问题

[英]Apache camel Junit mock issue

I am writing a JUnit test case for a Route class. 我正在为Route类编写一个JUnit测试用例。 I'm facing a problem while mocking ServiceClass inside the Processor class. 我在Processor类中模拟ServiceClass遇到了问题。

 public class SaveRouteTest extends CamelTestSupport {
    private Exchange exchange;
    protected ProducerTemplate template;
    private SaveRequestBuilder saveRequestBuilder;
    private SaveRoute route;
    private SaveProcessor saveProcessor;
    private ApplicationContext springContext = createApplicationContext();

    @Mock
    SaveServiceClient saveServiceClient;//Not able to mock this class
    @BeforeClass
    public void prepareTestCamelContext() throws Exception {
        route = springContext.getBean("saveRoute", saveRoute.class);
        saveProcessor = springContext.getBean("saveProcessor", 
      SaveProcessor.class);
        saveRequestBuilder = springContext.getBean("saveRequestBuilder", 
         SaveRequestBuilder.class);
    }
    @BeforeMethod
    public void init() throws SQLException, ServiceException {
        MockitoAnnotations.initMocks(this);
        exchange = new DefaultExchange(context);
    }
    @Override
    protected RouteBuilder[] createRouteBuilders() throws Exception {
        template = context.createProducerTemplate();
        return new RouteBuilder[]{route};
    }
    @Test
    public void testHotelCommitTransactionRouteSuccessReturn() throws 
   Exception {    

    when(saveServiceClient.invokeServiceWithName(anyObject()).
     thenReturn("Response");
        exchange.getIn().setBody("Request detail");
        exchange = template.send("direct:SaveRoute",exchange);
    }
    protected ApplicationContext createApplicationContext() {
        return new ClassPathXmlApplicationContext("classpath*:config/spring/testContext.xml");
    }
}

@Component
public class SaveRoute extends SPRouteBuilder {
    @Autowired
    private SaveProcessor saveProcessor;
    @Override
    public void configure() throws Exception {
        from("direct:SaveRoute")
                .routeId("save")
                .to("direct:ProcessSaveFlow")
                .end();
        from("direct:ProcessSaveFlow")
                        .process(saveProcessor)
        .end();
    }
}


public class SaveProcessor implements Processor {
    @Autowired
    SaveServiceClient saveServiceClient;
    @Override
    public void process(Exchange exchange) throws Exception {
        //This line of code not able to mock
        String response = saveServiceClient.invokeServiceWithName(exchange);
        exchange.getIn().setBody(response);
    }
}

How to resolve mocking of saveServiceClient.invokeServiceWithName ? 如何解决saveServiceClient.invokeServiceWithName The debugger is always going inside this method. 调试器总是在这个方法里面。 I tried using both mock objects and an injected mock. 我尝试使用模拟对象和注入模拟。 I can't make the method call directly. 我无法直接调用方法。

You are creating a mock object, however you are not injecting it anywhere (normally you are doing it with @InjectMocks annotation - read about it). 您正在创建一个模拟对象,但是您没有在任何地方注入它(通常您使用@InjectMocks注释进行注释 - 阅读它)。

I think there are several possibilities: 我认为有几种可能性:

  1. Provide a @MockBean object, which will be considered as a bean candidate in context. 提供一个@MockBean对象,该对象在上下文中将被视为bean候选对象。

There is a code example for mocking beans. 有一个模拟bean的代码示例。

    @RunWith ( CamelSpringRunner.class )
    @SpringBootTest
    public class RouteBuilderTest extends CamelSpringTestSupport {

    @Autowired
    private ApplicationContext applicationContext;

    @MockBean
    private ServiceClient serviceClient;

    @Override
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks( this );
        super.setUp();
    }

    @Override
    public void tearDown() {
    }

    @Test
    public void test() {

        when( serviceClient.doStuff() ).thenReturn( "mockedResponse" );
    }
    } 
  1. Mock SaveProcessor and inject it to Route class - you shouldn't take care of ServiceClient, because you are trying to test too much. 模拟SaveProcessor并将其注入到Route类 - 你不应该关注ServiceClient,因为你试图测试太多。 Tests for SaveProcessor should be separated, tests for route don't need this logic. SaveProcessor的测试应该是分开的,路由测试不需要这个逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM