【发布时间】:2016-08-25 18:30:30
【问题描述】:
有没有办法让我使用 MUnit 来结束测试并在调用子流时添加断言? 我想将测试分成更小的部分。
【问题讨论】:
有没有办法让我使用 MUnit 来结束测试并在调用子流时添加断言? 我想将测试分成更小的部分。
【问题讨论】:
您将不得不模拟子流程的结果。
为此,您必须使用 MUnit 中的 Mock 组件。
它的作用是,一旦调用子流/流引用,您就必须定义预期输出。
这是一个简单的例子:
<mock:when messageProcessor=".*:.*" doc:name="Mock Get Email Recipient">
<mock:with-attributes>
<mock:with-attribute name="doc:name" whereValue="#['getEmailRecipient']"/>
</mock:with-attributes>
<mock:then-return payload="hello@gmail.com" mimeType="text/plain"/>
</mock:when>
它的作用是模拟 getEmailRecipient 流以在测试中调用它时返回静态值 hello@gmail.com。
【讨论】:
不确定它是否有效,但也许您可以尝试使用模拟消息处理器模拟子流的结果,像这样
<mock:when messageProcessor="mule:sub-flow">
<mock:with-attributes>
<mock:with-attribute whereValue="<the_name_of_your_subflow>" name="doc:name"/>
</mock:with-attributes>
</mock:when>
参见文档here
希望对你有帮助。
/T
【讨论】:
正如他们所说,实现您想要的方法是使用模拟。 为了清楚起见,您不会停止测试,您只是定义要执行的行为而不是实际代码。断言将在您的测试后期出现。
以下是一些文档页面,我们将在此主题方面为您提供帮助:
最后,即使每个 MUnit 消息处理器在每个文档页面的底部都有一个 Java 示例,也有一个关于如何编写基于 Java 的 MUnit 测试的特定页面:
【讨论】:
文档here 解释了 xml 中的模拟流和子流。
要在 Java 中模拟子流,您应该在 withValue 中使用 org.mule.modules.interceptor.matchers.Matchers.contains 匹配器。
包含的参数应该是您的子流的名称或任何正则表达式模式字符串。
whenMessageProcessor("sub-flow")
.withAttributes(
Attribute.attribute("name")
.withValue(Matchers.contains("my-subflow-name")))
.thenThrow(new Exception("Test"));
同样的技巧也可以模拟verifyMessageProcessor 中的子流。
HTH!
【讨论】: