2

我有这样的路线

from("direct:start").beanRef("someBean");

对于单元测试,我尝试为其获取模拟端点,但不满足 expectedMessageCount 条件。

MockEndpoint beanMock = getMockEndpoint("mock:bean:someBean");
beanMock.expectedMessageCount(1);

如果我将路线更改为此,一切正常。

from("direct:start").to("bean:someBean");

以下也不起作用:

MockEndpoint beanMock = getMockEndpoint("mock:ref:someBean");

如何为 beanRef 获取正确的模拟端点?

4

1 回答 1

1

The problem is that beanRef does not produce an endpoint, so you can not access it by getMockEndPoint. if you want to test the result of your bean, you can add a mock endPoint and test its content.

here is an example:

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class BeanRefMock extends CamelTestSupport {
    public static class SomeBean{
        public void handle(Exchange ex){
            System.out.println("SomeBean : " +ex);
            ex.getIn().setBody(ex.getIn().getBody() +" is Processed By Bean");
        }
    }
    @Override
    protected CamelContext createCamelContext() throws Exception {
        CamelContext camelContext = super.createCamelContext();
        return camelContext;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.camel.test.junit4.CamelTestSupport#createRouteBuilder()
     */
    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:start").bean(SomeBean.class).to("mock:postBean");
            }
        };
    }

    @Test
    public void testQuoteCount() throws Exception {
        MockEndpoint mockEndpoint = getMockEndpoint("mock:postBean");
        mockEndpoint.expectedBodiesReceived("hello mock is Processed By Bean");

        template.sendBody("direct:start", "hello mock");
        mockEndpoint.assertIsSatisfied();
    }

}
于 2013-05-29T04:47:24.417 回答