Apache Camel Mock ProducerTemplate端点

时间:2017-10-06 09:39:42

标签: unit-testing apache-camel

我想模拟邮件发送不在单元测试中发送邮件。

我的代码如下:

@Component("utilityRoutes")
public class UtilityRoutes extends RouteBuilder {

  @Override
  public void configure() throws Exception {
    from("seda:sendEmail")
    .routeId("utilityRoutes")   
    .setHeader("from", simple("{{email.from}}"))        
    .setHeader("contentType", constant("text/plain;charset=UTF-8"))

    .choice()
        .when(header(Constants.HEADER_EMAIL_TARGET).isEqualTo("AAA"))
            .setHeader("to", simple("{{recipients-a}}"))
        .when(header(Constants.HEADER_EMAIL_TARGET).isEqualTo("BBB"))
            .setHeader("to", simple("{{recipients-b}}"))
    .end()

    .to("{{email.url}}")
    .id("emailUrl");

  }

}

我的测试案例是:

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@TestPropertySource(locations = "classpath:application.properties")
@ContextConfiguration(classes = { RailMLExport.class })
@DirtiesContext
public class UtilityRoutesTest extends CamelTestSupport {

  @Autowired
  CamelContext camelContext;

  @Produce(uri = "seda:sendEmail")
  ProducerTemplate producerTemplate;

  @EndpointInject(uri="mock:emailEndpoint")
  MockEndpoint mailEndpoint;

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

  @Override
  public String isMockEndpoints() {
    return "*";
  }

  @Test
  public void testSendAndReceiveMail() throws Exception {

    camelContext.getRouteDefinition("utilityRoutes").adviceWith(camelContext, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            weaveById("emailUrl").after().to("mock:emailEndpoint");
        }
    }

    );
    camelContext.start();
    Collection<Endpoint> endpoints = camelContext.getEndpoints();
    for (Endpoint endpoint : endpoints) {
        System.err.println(endpoint.getEndpointUri());
    }

    mailEndpoint.expectedMessageCount(1);
    mailEndpoint.expectedBodyReceived();

    Map<String,Object> headers = new HashMap<>();
    headers.put(Constants.HEADER_EMAIL_TARGET,"AAA");
    headers.put(Constants.HEADER_EMAIL_SUBJECT, "CCC");

    producerTemplate.requestBodyAndHeaders("seda:sendEmail", "test", headers);
    mailEndpoint.assertIsSatisfied();

  }
}

我给出如下错误。

java.lang.IllegalStateException: ProducerTemplate has not been started
  at org.apache.camel.impl.DefaultProducerTemplate.getProducerCache(DefaultProducerTemplate.java:689)
  at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:148)
  at org.apache.camel.impl.DefaultProducerTemplate.sendBodyAndHeaders(DefaultProducerTemplate.java:267)
  at org.apache.camel.impl.DefaultProducerTemplate.requestBodyAndHeaders(DefaultProducerTemplate.java:317)
  at org.apache.camel.impl.DefaultProducerTemplate.requestBodyAndHeaders(DefaultProducerTemplate.java:313)
  at fi.vr.h.ivu.integration.railmlexport.routes.UtilityRoutesTest.testSendAndReceiveMail(UtilityRoutesTest.java:89)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
  at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
  at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
  at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
  at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
  at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
  at org.junit.rules.RunRules.evaluate(RunRules.java:20)
  at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
  at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
  at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

1 个答案:

答案 0 :(得分:1)

如上所述覆盖isMockEndpointsAndSkip或使用adviceWith,并在AdviceWithRoute中跳过它。您甚至可以intercept your exchange并从那里拨打skipSendToOriginalEndpoint。不同的可能方式来实现你想要的。

进一步说明:您可能不应在"*"(或isMockEndpoints)中使用isMockEndpointsAndSkip,除非您想模拟每条路线。只需提及您真正想要模拟的路线。通过这种方式,其他程序员可以更清楚地看到你的代码是什么,你真正在嘲笑什么不是。