如何在骆驼中对石英进行单元测试

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在骆驼中对石英进行单元测试相关的知识,希望对你有一定的参考价值。

我有问题弄清楚如何测试我的路线使用quartz在其from我的spring文件

<spring:route id="metricExtractionTimer" startupOrder="1">
    <spring:from uri="quartz://metricExtractionTimer?fireNow=true"/>
    <transform>
        <simple>........... Object Added to DB ...........</simple>
    </transform>
    <to uri="stream:out"/>
    <to uri="mock:result"/>
</spring:route>

我的测试文件

@RunWith(CamelSpringJUnit4ClassRunner.class)
@BootstrapWith(CamelTestContextBootstrapper.class)
@ContextConfiguration(locations = "/beans-test.xml")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class MetricExtractionTest{

    @EndpointInject(uri = "mock:result")
    protected MockEndpoint resultEndpoint;

    @Produce(uri = "quartz://metricExtractionTimer?fireNow=true")
    protected ProducerTemplate producerTemplate;

    @Test
    public void testQuartzCron() throws Exception {
        producerTemplate.sendBody(null);
    }
}

例外:

Caused by: org.apache.camel.FailedToStartRouteException: Failed to start route metricExtractionTimer because of startupOrder clash. Route quartz already has startupOrder 1 configured which this route have as well. Please correct startupOrder to be unique among all your routes.
    at org.apache.camel.impl.DefaultCamelContext.doCheckStartupOrderClash(DefaultCamelContext.java:3414)
    at org.apache.camel.impl.DefaultCamelContext.safelyStartRouteServices(DefaultCamelContext.java:3364)
    at org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRoutes(DefaultCamelContext.java:3147)
    at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:3003)
    at org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:172)
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:2799)
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:2795)
    at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:2818)
    at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:2795)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
    at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:2764)
    at org.apache.camel.test.spring.CamelSpringTestContextLoader$7.execute(CamelSpringTestContextLoader.java:515)
    at org.apache.camel.test.spring.CamelSpringTestHelper.doToSpringCamelContexts(CamelSpringTestHelper.java:92)
    at org.apache.camel.test.spring.CamelSpringTestContextLoader.handleCamelContextStartup(CamelSpringTestContextLoader.java:509)
    at org.apache.camel.test.spring.CamelSpringTestContextLoader.loadContext(CamelSpringTestContextLoader.java:164)
    at org.apache.camel.test.spring.CamelSpringTestContextLoader.loadContext(CamelSpringTestContextLoader.java:87)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    ... 28 more
答案

看起来Camel在手动触发自触发路线时遇到麻烦,这是可以理解的。此外,您真的不想测试Camel Quartz模块,因为这已经过良好测试的开源代码。

我建议你让石英路线调用一个带有direct:yourRouteName consumer的子路线。然后可以从您的单元测试中顺利调用那个。

另一答案

此外,您真的不想测试Camel Quartz模块,因为这已经过良好测试的开源代码。

不同意本声明。根据这个推理,我们不应该测试我们与其他框架的交互,例如春天,冬眠,骆驼等

@Bilal:你最好的办法是看看camel-quartz2项目如何测试自己[1]。

具体来说,你可以看一下[2]和[3]。

[1] https://github.com/apache/camel/tree/master/components/camel-quartz2/src/test

[2] https://github.com/apache/camel/blob/master/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/SpringQuartzCronRouteTest.java

[3] https://github.com/apache/camel/blob/master/components/camel-quartz2/src/test/resources/org/apache/camel/component/quartz2/SpringQuartzCronRouteTest.xml

举个例子:

@Component
public class MyRoute extends RouteBuilder {

  @Override
  public void configure() throws Exception {
    from("quartz2://myGroup/myTimerName?cron=0/2+*+*+*+*+?")
        .to("mock:result");
  }
}


@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
public class MyRouteTest {

  @Autowired
  private CamelContext camelContext;

  @EndpointInject(uri = "mock:result")
  private MockEndpoint resultEndpoint;

  @Test
  public void testRoute() throws Exception {
    resultEndpoint.expectedMinimumMessageCount(3);

    MockEndpoint.assertIsSatisfied(camelContext);
  }
}

以上是关于如何在骆驼中对石英进行单元测试的主要内容,如果未能解决你的问题,请参考以下文章

如何在订阅Angular7单元测试用例中对代码进行单元测试

如何在 java 中对 jdbc 代码进行单元测试? [关闭]

如何在验证方法 nuxtjs vuejs jest 中对代码进行单元测试

如何在打字稿中对模型接口进行单元测试?

如何在 Flutter 中对依赖于 3rd-Party-Package 的代码进行单元测试?

如何在 Gatsby 中对 GraphQL 查询进行单元测试