我使用了一个骆驼示例作为用例:
https://svn.apache.org/repos/asf/camel/trunk/examples/camel-example-spring-ws/
假设我们想为这个项目创建一个单元测试:
package org.apache.camel.example.server;
import org.apache.camel.test.spring.CamelSpringTestSupport;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class IncrementRouteTest extends CamelSpringTestSupport {
@Override
protected AbstractApplicationContext createApplicationContext() {
return new FileSystemXmlApplicationContext(new String[]{
"/src/main/webapp/WEB-INF/spring-ws-servlet.xml"});
}
@Test
public void testSendMatchingMessage() throws Exception {
template.sendBodyAndHeader("spring-ws:rootqname:{http://camel.apache.org/example/increment}incrementRequest?endpointMapping=#endpointMapping",
"<msg>asd</msg>",
"head1",
"headVal1");
}
}
为了简化,我跳过额外的端点创建模拟它并验证结果是否满足..
在代码上运行将导致:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.apache.camel.example.server.IncrementRouteTest
2014-07-09 12:36:50,682 [main ] INFO IncrementRouteTest - ********************************************************************************
2014-07-09 12:36:50,683 [main ] INFO IncrementRouteTest - Testing: testSendMatchingMessage(org.apache.camel.example.server.IncrementRouteTest)
2014-07-09 12:36:50,683 [main ] INFO IncrementRouteTest - ********************************************************************************
2014-07-09 12:36:50,808 [main ] INFO ileSystemXmlApplicationContext - Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@911ed70: startup date [Wed Jul 09 12:36:50 CEST 2014]; root of context hierarchy
2014-07-09 12:36:50,865 [main ] INFO XmlBeanDefinitionReader - Loading XML bean definitions from file [/home/wojtek/repo/coi/camel/apache-camel-2.13.1/examples/camel-example-spring-ws/src/main/webapp/WEB-INF/spring-ws-servlet.xml]
2014-07-09 12:36:51,900 [main ] INFO DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@70d343dc: defining beans [template,consumerTemplate,camelContext:beanPostProcessor,camelContext,incrementRoute,myFileRoute,endpointMapping,loggingInterceptor,validatingInterceptor,increment]; root of factory hierarchy
2014-07-09 12:36:52,105 [main ] INFO PayloadValidatingInterceptor - Validating using file [/home/wojtek/repo/coi/camel/apache-camel-2.13.1/examples/camel-example-spring-ws/src/main/webapp/WEB-INF/increment.xsd]
2014-07-09 12:36:52,232 [main ] INFO SpringCamelContext - Apache Camel 2.13.1 (CamelContext: camelContext) is starting
2014-07-09 12:36:52,232 [main ] INFO ManagedManagementStrategy - JMX is enabled
2014-07-09 12:36:52,398 [main ] INFO DefaultTypeConverter - Loaded 181 type converters
2014-07-09 12:36:52,468 [main ] INFO SpringCamelContext - AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
2014-07-09 12:36:52,468 [main ] INFO 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
2014-07-09 12:36:52,470 [main ] INFO JaxbDataFormat - Creating JAXBContext with contextPath: org.apache.camel.example.server.model and ApplicationContextClassLoader: sun.misc.Launcher$AppClassLoader@13883d5f
2014-07-09 12:36:52,515 [main ] INFO SpringCamelContext - Route: route1 started and consuming from: Endpoint[spring-ws://rootqname:(http://camel.apache.org/example/increment)incrementRequest?endpointMapping=%23endpointMapping]
2014-07-09 12:36:52,515 [main ] INFO SpringCamelContext - Total 1 routes, of which 1 is started.
2014-07-09 12:36:52,520 [main ] INFO SpringCamelContext - Apache Camel 2.13.1 (CamelContext: camelContext) started in 0.283 seconds
2014-07-09 12:36:52,551 [main ] INFO IncrementRouteTest - ********************************************************************************
2014-07-09 12:36:52,551 [main ] INFO IncrementRouteTest - Testing done: testSendMatchingMessage(org.apache.camel.example.server.IncrementRouteTest)
2014-07-09 12:36:52,551 [main ] INFO IncrementRouteTest - Took: 0.029 seconds (29 millis)
2014-07-09 12:36:52,551 [main ] INFO IncrementRouteTest - ********************************************************************************
2014-07-09 12:36:52,552 [main ] INFO SpringCamelContext - Apache Camel 2.13.1 (CamelContext: camelContext) is shutting down
2014-07-09 12:36:52,552 [main ] INFO DefaultShutdownStrategy - Starting to graceful shutdown 1 routes (timeout 10 seconds)
2014-07-09 12:36:52,560 [ - ShutdownTask] INFO DefaultShutdownStrategy - Route: route1 shutdown complete, was consuming from: Endpoint[spring-ws://rootqname:(http://camel.apache.org/example/increment)incrementRequest?endpointMapping=%23endpointMapping]
2014-07-09 12:36:52,560 [main ] INFO DefaultShutdownStrategy - Graceful shutdown of 1 routes completed in 0 seconds
2014-07-09 12:36:52,572 [main ] INFO SpringCamelContext - Apache Camel 2.13.1 (CamelContext: camelContext) uptime 0.340 seconds
2014-07-09 12:36:52,572 [main ] INFO SpringCamelContext - Apache Camel 2.13.1 (CamelContext: camelContext) is shutdown in 0.020 seconds
2014-07-09 12:36:52,573 [main ] INFO ileSystemXmlApplicationContext - Closing org.springframework.context.support.FileSystemXmlApplicationContext@911ed70: startup date [Wed Jul 09 12:36:50 CEST 2014]; root of context hierarchy
2014-07-09 12:36:52,573 [main ] INFO DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@70d343dc: defining beans [template,consumerTemplate,camelContext:beanPostProcessor,camelContext,incrementRoute,myFileRoute,endpointMapping,loggingInterceptor,validatingInterceptor,increment]; root of factory hierarchy
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.06 sec <<< FAILURE! - in org.apache.camel.example.server.IncrementRouteTest
testSendMatchingMessage(org.apache.camel.example.server.IncrementRouteTest) Time elapsed: 1.901 sec <<< ERROR!
org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[Message: <msg>asd</msg>]
at org.apache.camel.component.spring.ws.SpringWebserviceProducer.process(SpringWebserviceProducer.java:89)
at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
at org.apache.camel.processor.UnitOfWorkProducer.process(UnitOfWorkProducer.java:73)
at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:378)
at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:346)
at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:242)
at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:346)
at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:184)
at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:124)
at org.apache.camel.impl.DefaultProducerTemplate.sendBodyAndHeader(DefaultProducerTemplate.java:163)
at org.apache.camel.impl.DefaultProducerTemplate.sendBodyAndHeader(DefaultProducerTemplate.java:159)
at org.apache.camel.example.server.IncrementRouteTest.testSendMatchingMessage(IncrementRouteTest.java:18)
经过一些调试后,我得到了更多信息..第89行 body = getEndpoint()。getConfiguration()。getWebServiceTemplate()。sendSourceAndReceive(sourcePayload,callback,SOURCE_EXTRACTOR);
getWebServiceTemplate()导致nullpointer ...
使用时效果很好:
package org.apache.camel.example.server;
import org.apache.camel.builder.RouteBuilder;
public class FileRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
from("file://target/inbox")
.to("file://target/outbox");
}
}
和内部测试:
package org.apache.camel.example.server;
import org.apache.camel.EndpointInject;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.spring.CamelSpringTestSupport;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class IncrementRouteTest extends CamelSpringTestSupport {
@Override
protected AbstractApplicationContext createApplicationContext() {
return new FileSystemXmlApplicationContext(new String[]{
"/src/main/webapp/WEB-INF/spring-ws-servlet.xml"});
}
@EndpointInject(uri = "mock:file:target/outbox/hello.txt")
protected MockEndpoint resultEndpoint;
@Test
public void testSendMatchingMessage() throws Exception {
template.sendBodyAndHeader("file://target/inbox",
"Hello World", "asd", "hello.txt");
resultEndpoint.assertIsSatisfied();
}
}
在SpringWebserviceProducer.java:89中可能是由NullPointer引起的 如何测试弹簧路线?