【问题标题】:Unit testing with Apache Camel使用 Apache Camel 进行单元测试
【发布时间】:2019-02-26 07:55:46
【问题描述】:

我想在骆驼路线下方进行测试。我在网上找到的所有示例都有以文件开头的路由,在我的例子中,我有一个 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();

【问题讨论】:

  • 如果你可以给 cmets 投反对票,那就合适了。
  • 我也不喜欢没有评论就投反对票。虽然我没有投反对票,但我想这是因为您的问题缺乏信息,并且您没有表明您已经努力自己寻找解决方案。无论哪种方式,我都提供了一个答案,它应该为您提供足够的提示,让您可以使用 junit 运行测试。
  • 我的回答可以帮助您完成第一部分(调用 qwartz 路由)stackoverflow.com/questions/47759037/testing-camel-quartz-route/…

标签: apache-camel spring-boot-test


【解决方案1】:

使用 Apache Camel 和 Spring-Boot 进行测试非常简单。

只需执行以下操作(下面的示例是一个抽象示例,只是为了提示您如何操作):

编写一个测试类

使用 Spring-Boot Annotations 配置测试类。

@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-Endpoints,如源和目标:

sourceEndpoint=direct:myTestSource

提示:

最好不要在使用 spring-boot 时直接在路由中硬连线你的 start-Endpoint,而是使用application.properties。这样可以更轻松地模拟单元测试的端点,因为您可以更改为 direct-Component 而无需更改源代码。

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

并在您的application.properties 中配置sourceEndpointsourceEndpoint=quartz2://tsTimer?cron=0/20+*+8-18+?+*+MON,TUE,WED,THU,FRI+*

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

文档

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

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

【讨论】:

    【解决方案2】:

    @NOD 之手 谢谢你的提示,我走错了方向。阅读您的答案后,我能够编写基本测试,并且我认为我可以将其向前推进。

    感谢您的宝贵时间,但是我发现根据我的路线,它应该将 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
    

    TradePublisherRoute.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);
        }
    

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

    【讨论】:

      猜你喜欢
      • 2012-03-21
      • 2017-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-31
      • 2020-03-07
      • 2022-06-29
      相关资源
      最近更新 更多