有没有办法让我用 MUnit 结束测试并在调用子流时添加断言?我想将测试分成更小的部分。
4 回答
您将不得不模拟子流程的结果。
为此,您必须使用 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>
在此处查看文档
希望能帮助到你。
/T
正如这些家伙所说,实现你想要的方法是使用模拟。为了清楚起见,您不会停止测试,您只是定义要执行的行为而不是实际代码。断言将在您的测试后期出现。
以下是一些文档页面,我们将在此主题方面为您提供帮助:
最后,即使每个 MUnit 消息处理器在每个文档页面的底部都有一个 Java 示例,也有一个关于如何编写基于 Java 的 MUnit 测试的特定页面:
Documentation here explains about mocking flow and sub-flow in xml.
To mock sub-flow in Java, you should use org.mule.modules.interceptor.matchers.Matchers.contains
matcher inside withValue
.
Arguments to contains should be name of your sub-flow or any regex pattern string.
whenMessageProcessor("sub-flow")
.withAttributes(
Attribute.attribute("name")
.withValue(Matchers.contains("my-subflow-name")))
.thenThrow(new Exception("Test"));
Same trick works to mock subflow in verifyMessageProcessor
too.
HTH!