Mule中的功能测试(Functional Testing)二
接上一篇Mule中的功能测试(Functional Testing)一
通知机制(Notifications)
通知机制用来替代事件回调,当一个消息到来的时候FunctionalTestComponent将触发一个通知告诉我们事件已经接收到了。
它将为我们在测试上建立一个监听(FunctionalTestNotificationListener)以致于我们能够捕获到通知。为了实现监听,我们必须
让我们的测试用例实现这个FunctionalTestNotificationListener接口。然后我们必须实现监听器暴露给我们的方法以至于我们能够被通知到。
以下的例子中我们校验notification.getAction看FunctionalTestNotification是否是由FunctionalTestComponent触发的。如果是,我们
将在控制台打印信息。
public void onNotification(ServerNotification notification)
{
if (notification.getAction() == FunctionalTestNotification.EVENT_RECEIVED)
{
System.out.println("Event Received");
}
}
为了让我们的监听器能够监听到通知,我们必须将它注册
muleContext.registerListener(this,"myComponent");
从组件FunctionalTestComponent返回模拟数据
FunctionalTestComponent能够返回无论是在文件中或者在Mule配置文件中指定的模拟数据,如这个例子返回了“donkey”信息,这个组件是这样配置的
<test:component>
<test:return-data>donkey</test:return-data>
</test:component>
如果从文件中返回,我们这样配置
<test:component>
<test:return-data file="abc.txt"/>
</test:component>
abc.txt文件必须存在路径下。FunctionalTestComponent其他的一些有用特性
强制异常(Forcing Exceptions)
你可以试用throwException来决定是否返回由exceptionToThrow指定的异常,如下配置:
<test:component throwException="true" exceptionToThrow="your.service.exception"/>
存储历史数据(Storing Message History)
默认情况下,每一条接收到的数据将被存储并且可被检索,如果你不想要这些信息被存储,你可以设置enableMessageHistory为false。
如果你通过组件跑着几百万条的信息的时候,这个特性又是开启的状态,那么内存溢出的异常就有可能发生。开启存储历史数据如下:
<test:component enableMessageHistory="true" />
信息都被存储在ArrayList中,如果你想要检索一条已经被存储的信息,你可以试用方法getReceivedMessage来返回指定顺序的信息
(e.g getReceivedMessage(1)将返回存储中的第一条信息),通过getLastReceivedMessage可以检索到我们接收到的最后一条信息。
你也可以通过getReceivedMessages检索到所有存储的信息。
增加文本到反馈(Appending Text to Responses)
你可以增加文本到反馈信息中,如下:
<test:component appendString="Received" />
延迟反馈(Delayed Responses)
你可以设置一个等待时间来延迟来自FunctionalTestComponent的信息,如下是设置延迟5秒
<test:component waitTime="5000" />
入境转换器失效(Disable Inbound Transformer)
你可以设置入境转换器失效
<test:component doInboundTransform="false" />
一些附加功能(Additional Features)
在这个功能包中包含了许多的附加功能,例如计算回调次数,一个测试回调能够计算接收到信息的数量,详细信息可以参考org.mule.tck.functional
Javadoc
测试组件配置参考(Test Component Configuration Reference)
以下是测试框架所能提供的测试组件的详细配置信息(mule-test.xsd)
组件(Component)
一个组件能够被使用于测试信息流中,这是一个配置组件,返回给组件的数据必须被设置使得用户能够模拟调用给一个真正的服务,此组件还可以跟踪调用历史
和当接收到信息时出发通知机制。
<componet...>属性
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| throwException | boolean | no | Whether the component should throw an exception before any processing takes place. | |
| logMessageDetails | boolean | no | Whether to output all message details to the log. This includes all headers and the full payload. The information will be loogged at INFO level. | |
| doInboundTransform | boolean | no | Whether the message will be transformed using the transformer(s) set on the inbound endpoint before it gets processed. The default is true. | |
| exceptionToThrow | name (no spaces) | no | A fully qualified classname of the exception object to throw. Used in conjunction with throwException. If this is not specified, a FunctionalTestException will be thrown by default. | |
| enableMessageHistory | boolean | no | Every message that is received by the test component is stored and can be retrieved. If you do not want this information stored, such as if you are running millions of messages through the component, you can disable this feature to avoid a potential out of memory error. | |
| enableNotifications | boolean | no | Whether to fire a FunctionalTestNotification when a message is received by the component. Test cases can register to receive these notifications and make assertions on the current message. | |
| appendString | string | no | A string value that will be appeneded to every message payload that passes through the component. Note that by setting this property you implicitly select that the message payload will be converted to a string and that a string payload will be returned. The inbound transformer (if any) will get applied first, but if that does not return a string, MuleEventContext.getMessageAsString() will be called directly after. | |
| waitTime | long | no | The time in milliseconds to wait before returning a result. All processing happens in the component before the wait begins. |
Child Elements of <component...>
| Name | Cardinality | Description |
|---|---|---|
| return-data | 0..1 | Defines the data to return from the service once it has been invoked. The return data can be located in a file, which you specify using the file attribute (specify a resource on the classpath or on disk), or the return data can be embeddded directly in the XML. |
| callback | 0..1 | A user-defined callback that is invoked when the test component is invoked. This can be useful for capturing information such as message counts. Use the class attribute to specify the callback class name, which must be an object that implements org.mule.tck.functional.EventCallback. |
webservice组件(Web Service Component)
一个用于测试webservice的组件,这个组件具有相同的属性,但是不同的是他实现了org.mule.api.lifecycle.Callable,org.mule.api.component.simple.EchoService,
org.mule.tck.testmodels.services.DateService, 和 org.mule.tck.testmodels.services.PeopleService。正如使用CXF一样使用WS endpoints的时候,必须确保serviceClass属性
已经被设置。Attributes of <web-service-component...>
Name Type Required Default Description throwException boolean no Whether the component should throw an exception before any processing takes place. logMessageDetails boolean no Whether to output all message details to the log. This includes all headers and the full payload. The information will be loogged at INFO level. doInboundTransform boolean no Whether the message will be transformed using the transformer(s) set on the inbound endpoint before it gets processed. The default is true. exceptionToThrow name (no spaces) no A fully qualified classname of the exception object to throw. Used in conjunction with throwException. If this is not specified, a FunctionalTestException will be thrown by default. enableMessageHistory boolean no Every message that is received by the test component is stored and can be retrieved. If you do not want this information stored, such as if you are running millions of messages through the component, you can disable this feature to avoid a potential out of memory error. enableNotifications boolean no Whether to fire a FunctionalTestNotification when a message is received by the component. Test cases can register to receive these notifications and make assertions on the current message. appendString string no A string value that will be appeneded to every message payload that passes through the component. Note that by setting this property you implicitly select that the message payload will be converted to a string and that a string payload will be returned. The inbound transformer (if any) will get applied first, but if that does not return a string, MuleEventContext.getMessageAsString() will be called directly after. waitTime long no The time in milliseconds to wait before returning a result. All processing happens in the component before the wait begins. Child Elements of <web-service-component...>
Name Cardinality Description return-data 0..1 Defines the data to return from the service once it has been invoked. The return data can be located in a file, which you specify using the file attribute (specify a resource on the classpath or on disk), or the return data can be embeddded directly in the XML. callback 0..1 A user-defined callback that is invoked when the test component is invoked. This can be useful for capturing information such as message counts. Use the class attribute to specify the callback class name, which must be an object that implements org.mule.tck.functional.EventCallback.

浙公网安备 33010602011771号