[Daily study]apache camel| BDD way to develop Apache camel| groovy| Spock

Source: Internet
Author: User
Tags apache camel

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.