我希望在单元测试中模拟邮件发送而不是真正发送邮件。
以下是我的代码:
@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)