JUnit在Camel路由上失败断言

时间:2015-08-24 22:28:27

标签: junit apache-camel blueprint

我正在为蓝图上编码的骆驼路线制作一个简单的Camel测试用例。 此路由将xml文件转换为Json格式。 路线做得很好,我对改造没有任何问题。 然后我也想在我的路线上学习和使用JUnit Test,通过制作一个Camel测试案例,但它在断言上失败了。我看不出我做错了什么,或者跟踪给我的差异。你能告诉我测试有什么问题吗?

接下来是我得到的Trace。

   java.lang.AssertionError: mock://output Message with body {"name" : "Frank Jarrod","custId" : "F475678678","vip" : 6,"stockId" : "ABC","shares" : 20,"cost" : null} was expected but not found in [{
  "name" : "Frank Jarrod",
  "custId" : "F475678678",
  "vip" : 6,
  "stockId" : "ABC",
  "shares" : 20,
  "cost" : null
}]
    at org.apache.camel.component.mock.MockEndpoint.fail(MockEndpoint.java:1394)

...

这是测试类:

import static org.junit.Assert.*;
import org.apache.camel.EndpointInject;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.blueprint.CamelBlueprintTestSupport;
import org.junit.Test;

/**
 * 
 */

/**
 * @author miguelangelmorales
 *
 */
public class Lab02XmlTest extends CamelBlueprintTestSupport {

    // TODO Create test message bodies that work for the route(s) being tested
    // Expected message bodies
    protected Object[] expectedBodies = {
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            + "<stocktrading>"
            + "<name>Frank Jarrod</name>"
            + "<custId>F475678678</custId>"
            + "<vip>6</vip>"
            + "<stockId>ABC</stockId>+"
            + "<shares>20</shares>+"
            + "</stocktrading>  "
                        };

    protected Object[] expectedResultBodies2 = {
            "{\"name\" : \"Frank Jarrod\",\"custId\" : \"F475678678\",\"vip\" : 6,\"stockId\" : \"ABC\",\"shares\" : 20,\"cost\" : null}" 
            };

    // Templates to send to input endpoints
    @Produce(uri = "file:myxml?delete=true")
    protected ProducerTemplate inputEndpoint;
    // Mock endpoints used to consume messages from the output endpoints and then perform assertions
    @EndpointInject(uri = "mock:output")
    protected MockEndpoint outputEndpoint;

    @Test
    public void testCamelRoute() throws Exception {
        // Create routes from the output endpoints to our mock endpoints so we can assert expectations
        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("file:donexml").to(outputEndpoint);
            }
        });

        // Define some expectations

        // TODO Ensure expectations make sense for the route(s) we're testing
        outputEndpoint.expectedBodiesReceivedInAnyOrder(expectedResultBodies2);

        // Send some messages to input endpoints
        for (Object expectedBody : expectedBodies) {
            inputEndpoint.sendBody(expectedBody);
        }

        // Validate our expectations
        assertMockEndpointsSatisfied();
    }

    @Override
    protected String getBlueprintDescriptor() {
        return "OSGI-INF/blueprint/Lab02.xml";
    }

}

0 个答案:

没有答案