标签:
开发apache camel应用,最好的方式就是tdd,因为camel的每个组件都是相互独立并可测试的。
现在有很多好的测试框架,用groovy的Spock框架的BDD(行为测试驱动)是比较优秀和好用的。
首先, 我们从最简单的processor开始。
先写测试用例:
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 MyProcessorTest extends 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("test");
def MyProcessor myProcessor=new MyProcessor()
when:"the body of exchange.in() is not null"
then:"set the body to exchange.out()"
def Exchange ex=myProcessor.doProcess(exchange)
ex.getIn().getBody(String.class)!=null
ex.getIn().getBody(String.class).equals("test")
}
}
以下是被测试的代码 :
package com.github.eric.camel;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
/**
* Created by eric567 on 4/2/2016.
*/
public class MyProcessor implements Processor {
@Override
public void process(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;
}
}
因为Processor本身没有返回值,这里加了一个方法:doProcess用来返回exchange 对象,使 MyProcessor变得更好测试。
运行上面的spock测试用例,应该可以看到令人激动的绿色。cheers!!!
[每日一学]apache camel|BDD方式开发apache camel|Groovy|Spock
标签:
原文地址:http://www.cnblogs.com/gyc567/p/5347778.html