代码之家  ›  专栏  ›  技术社区  ›  Jigar Naik

用apache camel进行单元测试

  •  -1
  • Jigar Naik  · 技术社区  · 6 年前

    我想在骆驼路线下测试。我在网上找到的所有示例都有从文件开始的路由,在我的例子中,我有一个spring bean方法,每隔几分钟调用一次,最后消息被转换并移动到jms以及审计目录。

    我对这条路线的笔试一无所知。 我目前在测试用例中所拥有的是 Mockito.when(tradeService.searchTransaction()).thenReturn(dataWithSingleTransaction);

    from("quartz2://tsTimer?cron=0/20+*+8-18+?+*+MON,TUE,WED,THU,FRI+*")
    .bean(TradeService.class)
    .marshal()
    .jacksonxml(true)
    .to("jms:queue:out-test")
    .to("file:data/test/audit")
    .end();
    
    0 回复  |  直到 6 年前
        1
  •  3
  •   the hand of NOD    6 年前

    使用apache camel和spring boot进行测试非常简单。

    只需做下面的例子(下面的例子是一个抽象的例子,只是给你一个提示,你可以怎么做):

    编写测试类

    使用spring boot注释来配置测试类。

    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
    @RunWith(SpringRunner.class)
    public class MyRouteTest {
        @EndpointInject(uri = "{{sourceEndpoint}}")
        private ProducerTemplate sourceEndpoint;
        ....
        public void test() {
            // send your body to the endpoint. See other provided methods too.
            sourceEndpoint.sendBody([your input]);
        }
    }
    

    src/test/application.properties : 配置camel端点,如源和目标:

    sourceEndpoint=direct:myTestSource
    

    提示:

    在使用SpringBoot时,最好不要在路由中直接硬连线起始端点,而是使用 application.properties . 这样,更容易模拟单元测试的端点,因为您可以更改为 direct -组件而不更改源代码。

    这意味着代替: from("quartz2://tsTimer?cron=0/20+*+8-18+?+*+MON,TUE,WED,THU,FRI+*") 你应该写: from("{{sourceEndpoint}}")

    并配置 sourceEndpoint 在你 应用程序.属性 : sourceEndpoint=quartz2://tsTimer?cron=0/20+*+8-18+?+*+MON,TUE,WED,THU,FRI+*

    这样你也可以在不同的情况下使用你的路线。

    文档

    有关如何使用Spring引导进行测试的好文档可以在这里找到: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html

    对于apache camel: http://camel.apache.org/testing.html

        2
  •  0
  •   Jigar Naik    6 年前

    “点头感谢你的暗示,我走向了完全错误的方向。”读了你的答案后,我就可以写基础测试了,我想我可以把它拿出来。

    感谢您的时间,但我看到,根据我的路线,它应该删除一个XML文件到审计目录,这是不发生的。

    看起来中间步骤也被嘲笑了,我没有指定任何内容。

    InterceptSendToMockEndpointStrategy - Adviced endpoint [xslt://trans.xslt] with mock endpoint [mock:xslt:trans.xslt]
    INFO  o.a.c.i.InterceptSendToMockEndpointStrategy - Adviced endpoint [file://test/data/audit/?fileName=%24%7Bheader.outFileName%7D] with mock endpoint [mock:file:test/data/audit/]
    INFO  o.a.camel.spring.SpringCamelContext - StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
    

    tradepublisherrout.java

        @Override
        public void configure() throws Exception {
            logger.info("TradePublisherRoute.configure() : trade-publisher started configuring camel route.");
    
            from("{{trade-publisher.sourceEndpoint}}")
            .doTry()
                .bean(tradeService)
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        String dateStr = Constant.dateFormatForFileName.format(new Date());
                        logger.info("this is getting executed : " + dateStr);
                        exchange.setProperty(Constant.KEY_INCOMING_XML_FILE_NAME, "REQ-" + dateStr + Constant.AUDIT_FILE_EXTENSION);
                        exchange.setProperty(Constant.KEY_OUTGOING_XML_FILE_NAME, "RESP-" + dateStr + Constant.AUDIT_FILE_EXTENSION);
                    }
                })
                .marshal()
                .jacksonxml(true)
                .wireTap("{{trade-publisher.requestAuditDir}}" + "${header.inFileName}")
                .to("{{trade-publisher.xsltFile}}")
                .to("{{trade-publisher.outboundQueue}}")
                .to("{{trade-publisher.responseAuditDir}}" + "${header.outFileName}")
                .bean(txnService, "markSuccess")
            .endDoTry()
            .doCatch(Exception.class)
                .bean(txnService, "markFailure")
                .log(LoggingLevel.ERROR, "EXCEPTION: ${exception.stacktrace}")
            .end();
    

    tradepublisherroutetest.java

    @ActiveProfiles("test")
    @RunWith(CamelSpringBootRunner.class)
    @SpringBootTest(classes = TradePublisherApplication.class)
    @MockEndpoints
    public class TradePublisherRouteTest {
    
        @EndpointInject(uri = "{{trade-publisher.outboundQueue}}")
        private MockEndpoint mockQueue;
    
        @EndpointInject(uri = "{{trade-publisher.sourceEndpoint}}")
        private ProducerTemplate producerTemplate;
    
        @MockBean
        TradeService tradeService;
    
        private List<Transaction> transactions = new ArrayList<>();
    
        @BeforeClass
        public static void beforeClass() {
    
        }
    
        @Before
        public void before() throws Exception {
            Transaction txn = new Transaction("TEST001", "C001", "100", "JPM", new BigDecimal(100.50), new Date(), new Date(), 1000, "P");
            transactions.add(txn);
    
        }
    
        @Test
        public void testRouteConfiguration() throws Exception {
            Mockito.when(tradeService.searchTransaction()).thenReturn(new Data(transactions));
            producerTemplate.sendBody(transactions);
            mockQueue.expectedMessageCount(1);
            mockQueue.assertIsSatisfied(2000);
        }
    

    如果我做错了什么,请纠正我!