【问题标题】:getExchange from mockEndPoint in a unit-test class for Camel Route Not Behaving As Expected骆驼路线的单元测试类中的 mockEndPoint 的 getExchange 行为不符合预期
【发布时间】:2017-09-20 15:28:26
【问题描述】:

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

这是我的单元测试类:

@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)

请帮我解决它。非常感谢。

【问题讨论】:

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


    【解决方案1】:

    您必须在获得交换之前声明模拟。因为这些交换是到达模拟的实际交换。所以它的期望必须首先得到满足,也就是说应该有 1 条消息到达。如果那是成功的,那么你可以通过索引 0 进行交换,你不会得到 IndexOutOfBoundsException

        MockEndpoint mockEndPointTest = getMockEndpoint("mock:catchCSVList");
        context.start();
    
        // set expectations on mock here
        mockEndPointTest.expectedMessageCount(1);
        mockEndPointTest.assertIsSatisfied();
    
        // okay now we can get the exchange's from the mock
        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));
    
        context.stop();
    

    【讨论】:

    • 哇,你是最棒的。我现在正在读你的书来学习 Apache Camel。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 2023-01-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    相关资源
    最近更新 更多