The best way to develop Apache camel applications is TDD, because each component of camel is independent and testable.
There are a lot of good test frameworks now, and the BDD (behavioral test Drive) with groovy Spock framework is relatively good and useful.
First, we start with the simplest processor.
Write Test Cases First:
Package Com.github.eric.camel
Import Org.apache.camel.Exchange
Import Org.apache.camel.impl.DefaultCamelContext
Import Org.apache.camel.impl.DefaultExchange
Import Spock.lang.Specification
/**
* Created by eric567 on 4/2/2016.
*/
Class MyprocessortestExtends Specification {
Def"Process" () {
Given"New myprocessor,new Exchange of NOT NULL body"
def Defaultcamelcontext Contex=defaultcamelcontext.newinstance ();
def Exchange exchange= Defaultexchange.newinstance (Contex)
Exchange.Getin ().Setbody ( def myprocessor myprocessor=new myprocessor ()
When:
Span style= "color: #6a8759;" >
Then:
def Exchange ex=myprocessor.doprocess (Exchange)
Ex.getin (). GetBody (String. Class)!=null
Ex. Getin (). GetBody (String. Class). Equals (
}
}
The following is the code being tested:
Package Com.github.eric.camel;
Import Org.apache.camel.Exchange;
Import Org.apache.camel.Processor;
/**
* Created by eric567 on 4/2/2016.
*/
public class MyprocessorImplements Processor {
@Override
public voidProcess (Exchange Exchange) throws Exception {
string in = Exchange.getin (string. Class);
if (in!=null)
{
Exchange.getout (). Setbody (in);
}
}
Public Exchange doprocess (Exchange Exchange) throws Exception {
Process (Exchange);
return Exchange;
}
}
Because processor itself has no return value, a method is added here: doprocess is used to return Exchange objects, making Myprocessor a better test.
Run the Spock test case above and you should see an exciting green. Cheers!!!
[Daily Study]apache camel| BDD way to develop Apache camel| groovy| Spock