Camel Route的单元测试类中的mockEndPoint的getExchange不符合预期

时间:2017-09-20 15:28:26

标签: unit-testing junit mocking apache-camel exchange-server

我想从Camel Route的单元测试类中的mockEndPoint获取交换,但它不起作用。

这是我的单元测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:camel-unit-test.xml")
public class ImportDatabaseRouteTest extends CamelTestSupport {
    @Value("${sql.importDatabase}")
    String oldEndPoint;

    @Autowired
    private ImportDatabaseRoute importDatabaseRoute;

    @Autowired
    private DriverManagerDataSource dataSource;

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return importDatabaseRoute;
    }

    @Before
    public void mockEndpoints() throws Exception {
        AdviceWithRouteBuilder adviceTest = new AdviceWithRouteBuilder() {
            @Override
            public void configure() throws Exception {
                interceptSendToEndpoint(oldEndPoint)
                        .skipSendToOriginalEndpoint()
                        .to("mock:catchCSVList");
            }
        };
        context.getRouteDefinitions().get(0).adviceWith(context, adviceTest);
    }

    @Override
    public boolean isUseAdviceWith() {
        return true;
    }

    @Override
    protected JndiRegistry createRegistry() throws Exception {
        JndiRegistry jndi = super.createRegistry();
        //use jndi.bind to bind your beans
        jndi.bind("dataSource", dataSource);
        return jndi;
    }
    @Test
    public void testTheImportRoute() throws Exception {
        MockEndpoint mockEndPointTest = getMockEndpoint("mock:catchCSVList");
        context.start();
        List<List<String>> test = (List<List<String>>) mockEndPointTest.getExchanges().get(0).getIn().getBody();
        assertEquals("4227",test.get(1).get(0));
        assertEquals("370",test.get(1).get(1));
        assertEquals("",test.get(1).get(2));
        mockEndPointTest.expectedMessageCount(1);
        mockEndPointTest.assertIsSatisfied();
        context.stop();
    }

}

以下是结果: java.lang.ArrayIndexOutOfBoundsException:0

at java.util.concurrent.CopyOnWriteArrayList.get(CopyOnWriteArrayList.java:387)

请帮我修理一下。非常感谢你。

1 个答案:

答案 0 :(得分:0)

在进行交换之前,你必须断言模拟。由于这些交换是到达模拟的实际交换。因此,它的期望必须首先满足,即1条消息应该到达。如果这是成功的话,那么你可以通过索引0获得那个交换,你就不会得到BEGIN; SELECT ... FROM Current ... FOR UPDATE; INSERT INTO History ...; UPDATE Current ...; COMMIT;

IndexOutOfBoundsException