Mule中的功能测试(Functional Testing)一

  由于Mule是一个轻量级且嵌入的,所以它很容易就可以运行在一个测试case的mule服务器,Mule提供了一个Junit的TestCase的抽象类org.mule.tck.FunctionalTestCase

,该类能够在Test Case中运行Mule,并且管理服务器的生命周期。在包org.mule.tck.functional包含了一些类来提供测试Mule代码的功能,

包括了类FunctionalTestComponent,这些类将在以后的章节中进行更加详细的描述。

FunctionalTestCase

  FunctionalTestCase是基于Test Case的为Mule提供了测试功能,你的测试用例可以通过继承它来使用测试功能。

  FunctionalTestCase通过你覆写getConfigResources()方法返回的配置文件中注册进Mule服务器。代码如下:

protected String getConfigResources()
{
return "mule-conf.xml";
}
注意:这边你可以通过getConfigResources方法返回一个配置文件,也可以返回一个用逗号分隔的配置文件列表,前提是这些配置文件都必须在你的路径下存在。
接下来你就可以创建测试用例与Mule服务器进行交互了。FunctionalTestCase 继承至 junit.framework.TestCase,所以Junit就是创建和运行你测试用例的框架。
如下一个简单的例子就是创建一个测试用例
来发送信息到VM
endpoint:
public void testSend() throws Exception
{
MuleClient client = new MuleClient(muleContext);
String payload = "foo";
Map<String, Object> properties = null;
MuleMessage result = client.send("vm://test", payload, properties);
assertEquals("foo Received", result.getPayloadAsString());
}
MuleClient就是用来与运行着的Mule服务器进行交互的,MuleClient就是用来发送信息,并且用它来接受信息来至你在配置文件中指定的端口endpoints(针对上面的例子
就是来自mule-conf.xml)
,以下展示了该例子中的配置文件的样子:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xmlns:test="http://www.mulesoft.org/schema/mule/test"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.0/mule.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.0/mule-vm.xsd
http://www.mulesoft.org/schema/mule/test http://www.mulesoft.org/schema/mule/test/3.0/mule-test.xsd">

<model name="TestComponentModel">
<service name="TestComponentService">
<inbound>
<inbound-endpoint address="vm://test"/>
</inbound>
<test:component appendString=" Received"/>
</service>
</model>
</mule>
监控超时watchdog timeout
在这个基本的测试用例中包括了监控超时的特性,当你的功能测试超过了60秒之后将会超时。为了更改这个超时设置,你可以添加-Dmule.test.timeoutSecs=XX
来通过mvn命令行来运行你的Mule也可以在你的IDE环境中使用Junit运行。截止
Mule 3.0-M2版本,你都可以通过设置环境变量MULE_TEST_TIMEOUTSECS来设置,
如果系统属性和环境变量都设置了,那么系统属性将优先。

FunctionalTestComponent

前面关于FunctionalTestCase的测试用例中涵盖了通用的(同步的)测试场景,即服务直接就返回给了调用者。
FunctionalTestComponent能够帮助你实现更丰富的测试用例,如下:
1 模拟异步通信
2 返回模拟数据给调用者
3 常见场景如强制抛出异常,存储历史信息,追加信息到反馈信息,延迟反馈。
该组件包括了两个方法onCall和
onReceive,他们基本上做着相同的事情:
* onCall:接收一个
MuleEventContext如一个输入和返回一个对象
* onReceive:接收一个对象如输入和返回一个对象
在以上的两个方法中,FunctionalTestComponent通过它(来自MuleEventContext或者来自一个Object对象)取得数据然后再转换成String。
接着它将创建一个消息返回给调用者,
它还检查是否有任何属性设置和相应的行为。

通过
FunctionalTestComponent来做异步测试
FunctionalTestComponent提供了两种异步反馈给调用者的机制:事件回调和通知机制。这两种机制都是通过注册监听器来触发事件处理
在功能测试过程中,监听器通常是一个访问了FunctionalTestCase的类。
事件回调
Event Callbacks
当测试组件被调用的时候自定义的事件回调被调用,如下例子给出了代码实现和配置:
public void testEventCallback() throws Exception 
{
EventCallback callback = new EventCallback()
{
public void eventReceived(MuleEventContext context, Object component)
throws Exception
{
System.out.println("Thanks for calling me back");
}
};

getFunctionalTestComponent("TestComponentService").setEventCallback(callback);

MuleClient client = new MuleClient();

client.send("vm://test", new DefaultMuleMessage("foo"));
}
在这个例子中eventReceived当FunctionalTestComponent接收到数据的时候被回调。
对应的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xmlns:test="http://www.mulesoft.org/schema/mule/test"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.0/mule.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.0/mule-vm.xsd
http://www.mulesoft.org/schema/mule/test http://www.mulesoft.org/schema/mule/test/3.0/mule-test.xsd">

<model name="TestComponentModel">
<service name="TestComponentService">
<inbound>
<inbound-endpoint address="vm://test"/>
</inbound>
<component>
<singleton-object class="org.mule.tck.functional.FunctionalTestComponent"/>
</component>
</service>
</model>
</mule>
在上面的配置文件中我们并没有使用"<test:component>",因为我们将FunctionalTestComponent设置成了单例,这样将使回调更好的工作。
再提供一个基于Spring组件的其他例子:
这个例子和上面的例子是一样的,只是这边使用了Spring组件,我们通过Spring注册来查找到该组件
public void testEventCallback() throws Exception 
{
EventCallback callback = new EventCallback()
{
public void eventReceived(MuleEventContext context, Object component)
throws Exception
{
System.out.println("Thanks for calling me back");
}
};

ApplicationContext ac =
(ApplicationContext)muleContext.getRegistry().lookupObject(SpringRegistry.SPRING_APPLICATION_CONTEXT);
FunctionalTestComponent testComponent = (FunctionalTestComponent) ac.getBean("FTC");
testComponent.setEventCallback(callback);

MuleClient client = new MuleClient();

client.send("vm://test", new DefaultMuleMessage("foo"));
}
对应的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xmlns:test="http://www.mulesoft.org/schema/mule/test"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.0/mule.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.0/mule-vm.xsd
http://www.mulesoft.org/schema/mule/test http://www.mulesoft.org/schema/mule/test/3.0/mule-test.xsd">

<spring:bean id="FTC" class="org.mule.tck.functional.FunctionalTestComponent" />

<model name="TestComponentModel">
<service name="TestComponentService">
<inbound>
<inbound-endpoint address="vm://test" />
</inbound>
<component>
<spring-object bean="FTC" />
</component>
</service>
</model>
</mule>


posted @ 2011-03-30 17:37  wuwenyu  阅读(979)  评论(0)    收藏  举报