...
Code Block | ||||
---|---|---|---|---|
| ||||
@ContextConfiguration(locations = "classpath:axis-example-context.xml")
public class ReportIncidentServiceTest extends AbstractJUnit38SpringContextTests {
|
What we must remember to add is the classpath: prefix as our xml file is located in src/main/resources
. If we omit the prefix then Spring will by default try to locate the xml file in the current package and that is org.apache.camel.example.axis. If the xml file is located outside the classpath you can use file: prefix instead. So with these two modifications we can get rid of all the setup and teardown code we had before and now we will test our real configuration.
...
Code Block | ||||
---|---|---|---|---|
| ||||
// get a producer template from the the spring context ProducerTemplate template = (ProducerTemplate) applicationContext.getBean("camelTemplate"); // inject it on our service using the setter service.setTemplate(template); |
So using this
...
Now our unit test is much better, and a real power of Camel is that is fits nicely with Spring and you can use standard Spring'ish unit test to test your Camel applications as well.
Unit Test calling WebService
What if you would like to execute a unit test where you send a webservice request to the AxisReportIncidentService how do we unit test this one? Well first of all the code is merely just a delegate to our real service that we have just tested, but nevertheless its a good question and we would like to know how. Well the answer is that we can exploit that fact that Jetty is also a slim web container that can be embedded anywhere just as Camel can. So we add this to our pom.xml:
Code Block | ||||
---|---|---|---|---|
| ||||
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>${jetty-version}</version>
<scope>test</scope>
</dependency>
|
Then we can create a new class AxisReportIncidentServiceTest to unit test with Jetty. The code to setup Jetty is shown below with code comments:
Code Block | ||||
---|---|---|---|---|
| ||||
public class AxisReportIncidentServiceTest extends TestCase {
private Server server;
private void startJetty() throws Exception {
// create an embedded Jetty server
server = new Server();
// add a listener on port 8080 on localhost (127.0.0.1)
Connector connector = new SelectChannelConnector();
connector.setPort(8080);
connector.setHost("127.0.0.1");
server.addConnector(connector);
// add our web context path
WebAppContext wac = new WebAppContext();
wac.setContextPath("/unittest");
// set the location of the exploded webapp where WEB-INF is located
// this is a nice feature of Jetty where we can point to src/main/webapp
wac.setWar("./src/main/webapp");
server.setHandler(wac);
// then start Jetty
server.setStopAtShutdown(true);
server.start();
}
@Override
protected void setUp() throws Exception {
super.setUp();
startJetty();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
server.stop();
}
}
|
TODO: Spring use onInit() to get the service once
TODO: Use spring dependency injection instead of property
TODO: Use Camel annotations
TODO: Maybe a route?
TODO: Use Camel Test Kit
TODO: Refer to Axis documentation. AXIS -> Axis.
TODO: Add more links to Camel stuff
TODO: Link to other tutorials