在骆驼测试中注入豆

时间:2016-03-03 17:39:30

标签: apache-camel

我有一个骆驼应用程序,看起来像下面的路线,如下所示: -

从("直接:getMarketplaceOrders&#34)。向("豆:orderHelper方法= getMarketplaceOrders&#34);

代码的入口点如下所示:

public class OrderMainApp {
    public static void main(String... args) throws Exception {
    OrderMainApp orderMainApp = new OrderMainApp();
    DefaultCamelContext camelContext = new DefaultCamelContext();
    ProducerTemplate producer = camelContext.createProducerTemplate();
    camelContext.setRegistry(orderMainApp.createRegistry(producer));
    camelContext.addRoutes(new OrderRouteBuilder(producer));
    camelContext.start();
    }

    protected JndiRegistry createRegistry(ProducerTemplate producer) throws Exception {
    JndiRegistry jndi = new JndiRegistry();
    OrderHelper orderHelper = new OrderHelper();
    orderHelper.setProducer(producer);
    jndi.bind("orderHelper", orderHelper);
    return jndi;
    }
}

OrderRouteBuilder configure中有如下路线: -

//processor is a custom JSONProcessor extending Processor
from("jetty:http://localhost:8888/orchestratorservice").process(processor);
from("direct:getMarketplaceOrders").to("bean:orderHelper?method=getMarketplaceOrders");

我的目标是在bean:orderHelper?method=getMarketplaceOrders

上发出请求时测试我从direct:getMarketplaceOrders收到的回复

orderHelper.getMarketplaceOrders如下所示: -

public OrderResponse getMarketplaceOrders(GetMarketplaceOrdersRequest requestParam) throws Exception

我的测试类看起来如下: -

public class OrderMainAppTest extends CamelTestSupport {

    @Produce(uri = "direct:getMarketplaceOrders")
    protected ProducerTemplate template;

    @EndpointInject(uri = "bean:orderHelper?method=getMarketplaceOrders")
    protected MockEndpoint resultEndpoint;

    @Test
    public void testSendMatchingMessage() throws Exception {
    String expectedBody = "<matched/>";

    template.sendBody("{\"fromDateTime\": \"2016-01-11 10:12:13\"}");

    resultEndpoint.expectedBodiesReceived(expectedBody);

    resultEndpoint.assertIsSatisfied();
    }

    @Override
    protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {
        @Override
        public void configure() {
        from("direct:getMarketplaceOrders").to("bean:orderHelper?method=getMarketplaceOrders");
        }
    };
    }
}

每当我运行测试时,我都会遇到以下异常: -

java.lang.IllegalArgumentException: Invalid type: org.apache.camel.component.mock.MockEndpoint which cannot be injected via @EndpointInject/@Produce for: Endpoint[bean://orderHelper?method=getMarketplaceOrders]

我猜这是因为我无法将OrderHelper传递给camel测试上下文。有人可以告诉我如何在模拟结果端点注入bean?

修改: -

我尝试按如下方式修改我的测试类: -

    public class OrderMainAppTest extends CamelTestSupport {

        protected OrderHelper orderHelper = new OrderHelper();

        @Produce(uri = "direct:getMarketplaceOrders")
        protected ProducerTemplate template;

        @EndpointInject(uri = "mock:intercepted")
        MockEndpoint mockEndpoint;


        @Before
        public void preSetup() throws Exception {
        orderHelper.setProducer(template);
        };

        @Test
        public void testSendMatchingMessage() throws Exception {

        GetMarketplaceOrdersRequest request = new GetMarketplaceOrdersRequest();
        request.setFromDateTime("2016-01-11 10:12:13");
        request.setApikey("secret_key");
        request.setMethod("getMarketplaceOrders");
        request.setLimit(10);
        request.setOffset(2);

        template.sendBody(request);
        mockEndpoint.expectedBodiesReceived("{\"success\":\"false\"");
        }

        @Override
        protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            @Override
            public void configure() {
interceptSendToEndpoint("bean:orderHelper?method=getMarketplaceOrders")
        .to("mock:intercepted");            from("direct:getMarketplaceOrders").to("bean:orderHelper?method=getMarketplaceOrders");
            }
        };
        }

        @Override
        protected JndiRegistry createRegistry() throws Exception {
        return getRegistry();
        }

        protected JndiRegistry getRegistry() {
        JndiRegistry jndi = new JndiRegistry();
        jndi.bind("orderHelper", orderHelper);
        return jndi;
        }
    }

上面的代码正确地发出请求,正在正确地流经我的应用程序。但是我无法拦截orderHelper.getMarketplaceOrders的回复。上面的代码只拦截请求。我尝试过更改为template.requestBody(request)。但仍然没有运气。

2 个答案:

答案 0 :(得分:1)

此错误表示您无法将bean:个终结点注入MockEndpoint

如果你想&#34;拦截&#34;拨打OrderHelper电话,您可以在路线中使用interceptSendToEndpoint

@EndpointInject(uri = "mock:intercepted")
MockEndpoint mockEndpoint;

...

@Override
protected RouteBuilder createRouteBuilder() {
  return new RouteBuilder() {
    @Override
    public void configure() {

      interceptSendToEndpoint("bean:orderHelper?method=getMarketplaceOrders")
        .to("mock:intercepted");

      from("direct:getMarketplaceOrders")
        .to("bean:orderHelper?method=getMarketplaceOrders");
  }
};

请参阅:http://camel.apache.org/intercept.html

答案 1 :(得分:0)

更新我的createRouteBuilder,如下所示。我能够拦截响应并将其发送到模拟端点,在那里我可以进行断言。

@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
    @Override
    public void configure() {
    from("direct:getMarketplaceOrders").to("bean:orderHelper?method=getMarketplaceOrders").onCompletion()
        .to("mock:intercepted");
    }
};
}