[每日一学]apache camel|BDD方式开发apache camel|Groovy|Spock
开发apache camel应用,最好的方式就是tdd,因为camel的每个组件都是相互独立并可测试的。
现在有很多好的测试框架,用groovy的Spock框架的BDD(行为测试驱动)是比较优秀和好用的。
首先, 我们从最简单的processor开始。
在maven工程文件pom.xml添加spock的依赖包:
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.0-groovy-2.4</version>
<scope>test</scope>
</dependency>
完整的依赖包信息为:
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.16.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jms</artifactId>
<version>2.16.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
<version>2.16.0</version>
</dependency>
<!-- the ActiveMQ client with connection pooling -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-camel</artifactId>
<version>5.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.12.0</version>
</dependency>
<!-- the ActiveMQ broker is optional and can be removed if connecting to a remote broker only -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
<version>5.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-spring</artifactId>
<version>5.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-kahadb-store</artifactId>
<version>5.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>3.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- testing -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-spring</artifactId>
<version>2.16.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-spring</artifactId>
<classifier>plugin</classifier>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.0-groovy-2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
</dependencies>
先写测试用例:
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 ,new Context instance "
def DefaultCamelContext contex = DefaultCamelContext.newInstance();
def Exchange exchange = DefaultExchange.newInstance(contex)
def MyProcessor myProcessor = new MyProcessor()
when: "the body of exchange.in() is test"
exchange.getIn().setBody("test");
then: "the body to exchange.out() ins not null ,equals test"
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!!!
本人精通java高并发,DDD,微服务等技术实践,专注java,rust技术栈。 本人姓名郭莹城,坐标深圳,前IBM架构师、咨询师、敏捷开发技术教练,前IBM区块链研究小组成员、十多年架构设计工作经验,《区块链核心技术与应用》作者之一, 现聚焦于:区块链创投与交易所资源对接和技术咨询。 工作微信&QQ:360369487,区块链创投与交易所资源对接,加我注明:博客园+对接,技术咨询和顾问,加我注明:博客园+顾问。想学习golang和rust的同学,也可以加我微信,备注:博客园+golang或博客园+rust,谢谢!