...
Code Block |
---|
dir target /b
123.txt
|
Unit Testing
We would like to be able to unit test our ReportIncidentService class. So we add junit to the maven dependency:
Code Block |
---|
|
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
<scope>test</scope>
</dependency>
|
And then we create a plain junit testcase for our service class.
Code Block |
---|
|
package org.apache.camel.example.axis;
import junit.framework.TestCase;
import org.apache.camel.example.reportincident.InputReportIncident;
import org.apache.camel.example.reportincident.OutputReportIncident;
/**
* Unit test of service
*/
public class ReportIncidentServiceTest extends TestCase {
public void testIncident() {
ReportIncidentService service = new ReportIncidentService();
InputReportIncident input = createDummyIncident();
OutputReportIncident output = service.reportIncident(input);
assertEquals("OK", output.getCode());
}
protected InputReportIncident createDummyIncident() {
InputReportIncident input = new InputReportIncident();
input.setEmail("davsclaus@apache.org");
input.setIncidentId("12345678");
input.setIncidentDate("2008-07-13");
input.setPhone("+45 2962 7576");
input.setSummary("Failed operation");
input.setDetails("The wrong foot was operated.");
input.setFamilyName("Ibsen");
input.setGivenName("Claus");
return input;
}
}
|
Then we can run the test with maven using: mvn test
. But we will get a failure:
Code Block |
---|
Running org.apache.camel.example.axis.ReportIncidentServiceTest
Hello ReportIncidentService is called from Claus
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.235 sec <<< FAILURE!
Results :
Tests in error:
testIncident(org.apache.camel.example.axis.ReportIncidentServiceTest)
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
|
What is the problem? Well our service uses a CamelProducer (the template) to send a message to the file endpoint so the message will be stored in a file. What we need is to get hold of such a producer and inject it on our service, by calling the setter.
Since Camel is very light weight and embedable we are able to create a CamelContext and add the endpoint in our unit test code directly. We do this to show how this is possible:
Code Block |
---|
|
private CamelContext context;
@Override
protected void setUp() throws Exception {
super.setUp();
// CamelContext is just created like this
context = new DefaultCamelContext();
// then we can create our endpoint and set the options
FileEndpoint endpoint = new FileEndpoint();
// the endpoint must have the camel context set also
endpoint.setCamelContext(context);
// our output folder
endpoint.setFile(new File("target"));
// and the option not to append
endpoint.setAppend(false);
// then we add the endpoint just in java code just as the spring XML, we register it with the "backup" id.
context.addSingletonEndpoint("backup", endpoint);
// finally we need to start the context so Camel is ready to rock
context.start();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
// and we are nice boys so we stop it to allow resources to clean up
context.stop();
}
|
So now we are ready to set the ProducerTemplate on our service, and we get a hold of that baby from the CamelContext as:
Code Block |
---|
|
public void testIncident() {
ReportIncidentService service = new ReportIncidentService();
// get a producer template from the camel context
ProducerTemplate template = context.createProducerTemplate();
// inject it on our service using the setter
service.setTemplate(template);
InputReportIncident input = createDummyIncident();
OutputReportIncident output = service.reportIncident(input);
assertEquals("OK", output.getCode());
}
|
And this time when we run the unit test its a success:
Code Block |
---|
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
|
We would like to test that the file exists so we add these two lines to our test method:
Code Block |
---|
|
// should generate a file also
File file = new File("target/" + input.getIncidentId() + ".txt");
assertTrue("File should exists", file.exists());
|
TODO: How to embed Jetty and run a unit test by hitting a webservice call and expect the file output
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