我有一个流程,我想测试如下:
<flow
name="MyFlow"
processingStrategy="synchronous">
<all>
<processor-chain>
<and-filter>
<filter ref="Filter1" />
<filter ref="Filter2" />
<filter ref="Filter3" />
</and-filter>
<!-- bla bla bla. doesnt matter for this question -->
</processor-chain>
<!-- more stuff that doesnt matter -->
</all>
</flow>
过滤器 1、2 和 3 都是自定义过滤器。
我在使用过滤器 2 时遇到问题,因为它有一些我无法在单元测试环境中复制或执行的集成代码。无论如何告诉我的测试只接受该过滤器会很棒。
我只是尝试了这样的事情(我的测试类正在扩展 FunctionalTestCase):
muleContext.getRegistry().registerObject(
"Filter2",
new FilterThatJustAcceptsEverything());
但是,那是行不通的。
是否有一种方法可以仅针对此特定测试模拟该过滤器或其代码的某些部分?
这就是我在测试方法中调用它的方式:
flow = muleContext.getRegistry().lookupObject(
"MyFlow");
String content = "bananas";
MuleEvent result = flow.process(getTestEvent(content));
提前致谢。
最佳答案
对于模拟,最好的方法是使用 munit - https://github.com/mulesoft/munit
您可以在其中模拟过滤器以仅返回相同的事件,使其看起来只是通过。类似于:
whenMessageProcessor("expression-filter").withAttributes(...).thenReturnSameEvent();
或者,如果您想使用标准的 FunctionalTestCase,我建议您使用并排配置 - http://www.mulesoft.org/documentation/display/current/Using+Side-by-Side+Configuration+Files并提供 stub 配置。
看起来您的过滤器已经是全局的了。所以你可以做的是将你的过滤器放在一个单独的配置“filters.xml”中,并在你的测试用例中提供一个“filters-stubs.xml”。它们可以只是像这样总是返回 true 的通用过滤器:
<expression-filter expression="#[true]" name="Filter1" />
并在您的测试中改为加载 stub 配置:
@Override
protected String getConfigResources()
{
return "main.xml, filters-stubs.xml";
}
关于unit-testing - 在 Mule 中的流单元测试中模拟过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22566152/