0

我有两条非常简单的路线(下面的代码不是原始类,我已经简化了它,比如删除设置器、日志等)

第一条路线:

public static final String MESSAGE_CONSUMER = "vm:myMessage";

public String myXmlProducer; //and getter method then

myXmlProducer = "file:myFileLocation?autoCreate=true&fileName="+ myFileName;
from(MESSAGE_CONSUMER)
    //some process here
    .to(myXmlProducer);

第二个:

public String fileConsumer = "file:myFileLocation";
public String ftpProducer = "ftp://ftpIp?username=username&password=password&maximumReconnectAttempts=0";

from(fileConsumer)
    .to(ftpProducer );

我正在尝试为这两条路线编写一些测试,这是我的测试课程的一部分,

类中的配置部分:

public static final String MOCK_OUT = "mock:out";
public static final String DIRECT_IN = "direct:in";
public static final String MOCK_XML_URI = "mock:xmlFile";
...
@EndpointInject(uri = MOCK_OUT)
MockEndpoint mockOut;

@EndpointInject(uri = MOCK_XML_URI)
MockEndpoint mockXmlUri;
...

@Override
public RouteBuilder[] createRouteBuilders() {
    final MyRoute route = new MyRoute();
    route.setFtpProducer (MOCK_OUT);

    RouteBuilder testHarness = new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from(DIRECT_IN).routeId("testHarness.in")
                .to(MyRoute.MESSAGE_CONSUMER)

            from(route.getMyXmlProducer())
                .convertBodyTo(Byte.class)
                .to(mockXmlUri)
        }
    };
    return new RouteBuilder[] {route, testHarness};
}

@Before
public void getTestData() throws IOException {
    testInputData = IOUtils.toString(this.getClass().getResourceAsStream("/myTarget/inputData.txt"));
}

这个测试方法是绿色的:

@Test
public void testRoutingOk() throws InterruptedException {
    mockOut.setExpectedMessageCount(1);
    mockOut.message(0).body().isEqualTo(testInputData);

    template.sendBody("file:myTestFileLocation", testInputData);
    assertMockEndpointsSatisfied();
}

问题是以下测试,我对骆驼很陌生,并且已经用谷歌搜索找到原因,但我自己无法弄清楚:

@Test
public void testRoutingOk() throws InterruptedException {
    mockXmlUri.setExpectedMessageCount(1);
    mockXmlUri.message(0).body().isEqualTo(testInputData);

    template.sendBody(DIRECT_IN, testInputData);
    assertMockEndpointsSatisfied();
}

我收到资产错误,根本没有消息:

java.lang.AssertionError: mock://xmlFile Received message count. Expected: <1> but was: <0>

有没有人可以帮助我,拜托。

4

1 回答 1

0

经过几天的努力,我发现了问题所在。我正在向正文发送一条消息,但是这条消息将被另一条路线使用。我使用的是 2.10 版本,绿色单元测试的解决方案是停止路由。使用当前版本(2.10)会有点复杂,因为我应该为了单元测试而改变我的主类。但是我可以使用 controlbus: 相反,如果我们将骆驼升级到 2.11+。

于 2015-08-12T12:46:53.933 回答