如何使用 MUnit 在 Mule Flow 中模拟 Java 组件

2023-12-14

我正在尝试使用 MUnit 对我的一个子流程进行单元测试,我需要模拟一个自定义 Java 组件,但我无法这样做。

我的子流程如下

Subflow under test

<sub-flow name="ProcessCSVFlow" tracking:enable-default-events="true">
    <transformer ref="enrichWithHeaderAndEndOfFileTransformer" doc:name="headerAndEOFEnricher" />
    <set-variable variableName="outputfilename" value="#['Mercury'+server.dateTime.year+server.dateTime.month+server.dateTime.dayOfMonth+server.dateTime.hours +server.dateTime.minutes+server.dateTime.seconds+'.csv']" doc:name="outputfilename"/>
    <sftp:outbound-endpoint exchange-pattern="one-way" connector-ref="DestinationSFTP" host="${destination.host}" port="22" responseTimeout="10000" doc:name="DestinationSFTP"
    outputPattern="#[outputfilename]" path="${destination.path}" user="${destination.username}" password="${destination.password}"/>
    <!-- <component class="nz.co.mightyriver.creditreport.component.ArchiveOutputComponent" doc:name="Archive"/> -->
    <gzip-compress-transformer/>
    <sftp:outbound-endpoint exchange-pattern="one-way" connector-ref="InputSFTP" host="${source.host}" port="22" responseTimeout="10000" doc:name="SourceArchiveSFTP" 
    outputPattern="#[outputfilename].gzip" path="Archive" user="${source.username}" password="${source.password}"/>
    <component doc:name="Delete Read File">
        <singleton-object class="nz.co.mightyriver.creditreport.component.DeleteProcessedFileComponent">
            <property key="host" value="${source.host}"/>
            <property key="username" value="${source.username}"/>
            <property key="password" value="${source.password}"/>
            <property key="workingDirectory" value="${source.path}"/>
        </singleton-object>
    </component>
    <parse-template location="successmessagetemplate.txt" doc:name="Success Template"/>
    <smtp:outbound-endpoint host="${smtp.host}" port="${smtp.port}" user="${smtp.from.address}" password="${smtp.from.password}" 
                            to="${smtp.to.address}" from="${smtp.from.address}" subject="${mail.success.subject}" responseTimeout="10000" 
                            doc:name="SuccessEmail" connector-ref="Gmail"/>
    <logger message="Process completed successfully" level="INFO" doc:name="Logger"/>
</sub-flow>

这是我失败的单元测试

@Test
public void givenAValidPayload_whenFlowIsInvoked_itShouldSendPayloadToDestinationSFTPOnlyOnce() throws Exception {
    destinationSFTP.thenReturnSameEvent();
    sourceArchiveSFTP.thenReturnSameEvent();
    deleteProcessedFileComponent.thenReturnSameEvent();
    successEmail.thenReturnSameEvent();


    MuleEvent testEvent = PropertyEnricher.enrich(testEvent(IOUtils.toInputStream("hello,dummy,payload"))).get();

    runFlow(PROCESS_CSV_FLOW, testEvent);

    verifyCallOfMessageProcessor("outbound-endpoint")
            .ofNamespace("sftp")
            .withAttributes(attribute("name").ofNamespace("doc").withValue("DestinationSFTP"))
            .times(1);

}

我尝试模拟该组件

deleteProcessedFileComponent = whenMessageProcessor("component")
            .withAttributes(attribute("name").ofNamespace("doc").withValue("Delete Read File"));

我尝试了一些变体,但没有一个起作用,我猜组件不是消息处理器

我得到的异常如下

org.mule.api.lifecycle.InitialisationException: Component has not been initialized properly, no flow constuct.
at org.mule.component.AbstractComponent.initialise(AbstractComponent.java:218)
at org.mule.processor.chain.AbstractMessageProcessorChain.initialise(AbstractMessageProcessorChain.java:80)
at org.mule.processor.chain.AbstractMessageProcessorChain$$FastClassByCGLIB$$38c17d88.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
at org.mule.munit.common.mp.WrapperMunitMessageProcessorInterceptor.invokeSuper(WrapperMunitMessageProcessorInterceptor.java:71)
at org.mule.munit.common.mp.WrapperMunitMessageProcessorInterceptor.invokeSuper(WrapperMunitMessageProcessorInterceptor.java:63)
at org.mule.munit.common.mp.WrapperMunitMessageProcessorInterceptor.intercept(WrapperMunitMessageProcessorInterceptor.java:49)
at org.mule.processor.chain.SubflowInterceptingChainLifecycleWrapper$$EnhancerByMUNIT$$c8ca2508.initialise(<generated>)
at org.mule.munit.runner.functional.FunctionalMunitSuite.initialiseSubFlow(FunctionalMunitSuite.java:269)
at org.mule.munit.runner.functional.FunctionalMunitSuite.runFlow(FunctionalMunitSuite.java:259)
at nz.co.mightyriver.ProcessCsvTest.givenAValidPayload_whenFlowIsInvoked_itShouldSendPayloadToDestinationSFTPOnlyOnce(ProcessCsvTest.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

所以你的模拟没有问题,它实际上工作得很好。 问题出在 Mule 和 MUnit 上。

简短的回答,目前还没有解决办法。 不过有一个解决方法!

创建一个新的 xml(以便将其与生产代码分开) 将流(不是子流)添加到该新 xml。 在您刚刚添加的流程中,对子流程执行流程引用。 从您的测试来看,不要执行 runFlow(PROCESS_CSV_FLOW),而是执行 runFlow("the_flow_you_created")。

更长的答案是:

子流不是真正的流,它们甚至不共享父类。因此他们的行为有点不同。 MUnit 做了一些事情来使这一事实对用户透明。

事实证明我们做得不够,我将在 MUnit 项目中创建一个 jira 来解决这个问题。但希望您能够继续进行测试。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 MUnit 在 Mule Flow 中模拟 Java 组件 的相关文章

随机推荐

  • 使用 qsort 对 2D 数组进行排序

    我正在尝试对二维数组进行排序 首先我按列排序 然后按行排序 逐列有效 但逐行无效 这段代码有什么问题 int scmpr const void a const void b return strcmp const char a const
  • 将 Rcpp 对象分配到 Rcpp 列表中会产生最后一个元素的重复项

    我正在尝试采取Rcpp CharacterMatrix并将每一行转换为它自己的元素Rcpp List 然而 我为此编写的函数有一个奇怪的行为 其中列表的每个条目对应于矩阵的最后一行 为什么会这样呢 这是一些与指针相关的概念吗 请解释 功能
  • 异常“PDOException”,消息“无法找到驱动程序”SQLSRV Laravel Artisan CLI

    我在 Windows Server 2008 上运行 php 5 5 我在应用程序中使用 Laravel 4 0 框架 我通过 PDO 连接到 SQL SRV 数据库 当通过 http 执行时没有问题 建立连接数据被调用和更新没有问题 但是
  • Firebase云功能“您的客户端无权从此服务器获取URL /200”

    我刚刚做了一个 firebase 云函数 exports deleteAfterSevenDays functions https onRequest req res gt 我部署了该函数并获得了一个函数 URL 当我从浏览器请求此网址时
  • 尝试在 Bluemix 上使用 FUSE 时出现问题

    我正在寻找一种方法来添加可访问 Bluemix 的远程文件系统 在这个post我被告知要使用 Cloud Foundry 最新版本支持的 cflinuxfs2 堆栈 我能够从我的 Java 应用程序执行挂载点的 mkdir 命令并执行 ss
  • Google Drive SDK - 上传图像、OCR、下载结果

    所以最终我尝试上传我想要 Google 进行 OCR 的图像 然后我希望能够将 OCR 结果传回我的 Android 应用程序 我的图像上传正常 我可以循环浏览我的谷歌驱动器中的所有文件 我看到有可用的导出链接 其中之一是 text pla
  • Angular ng-view/路由在 PhoneGap 中不起作用

    我在 PhoneGap 中使用 ngView 时遇到问题 一切似乎都加载得很好 我什至可以使用 ng controller 获得一个基本的控制器 但是当我尝试将路由与 ngView 一起使用时 没有任何反应 索引 html
  • 如何在java中接收yyyyMMddHHmm格式的输入?

    SimpleDateFormat sdf new SimpleDateFormat yyyyMMddHHmm java util Date parsedDate sdf parse 201212130900 java sql Timesta
  • “new Random(x)”总是生成相同的数字? [复制]

    这个问题在这里已经有答案了 我试图获得一个唯一的随机数 但每次运行代码时我总是得到相同的数字 我将先得到 14 然后得到 6 但我保存所有已用数字的列表似乎不起作用 手动添加 14 可以 但当我添加 randInt 时 它不起作用 cons
  • React onClick 和 PreventDefault() 链接刷新/重定向?

    我正在使用 React 渲染链接 render gt a upvote a 然后 上面我有 upvote 功能 upvote gt do stuff ajax 在链接之前 我在那个地方有跨度 但我需要切换到链接 这就是麻烦 每次我点击 up
  • 从 SignalR 中心发送异步邮件

    我需要发送一封电子邮件作为 SignalR 中心调用的结果 我不希望发送同步执行 因为我不想绑定 WebSocket 连接 但如果可能的话 如果有任何错误 我希望通知调用者 我想我可以在集线器中使用类似的东西 减去错误处理和我想要它做的所有
  • 获取运行 JVM 的 GC 设置 [重复]

    这个问题在这里已经有答案了 有没有办法获取正在运行的 JVM 的 GC 设置 我正在尝试查看运行 SerialGC ParallelGC ParallelOldGC ConcurrentMarkSweepGC 等的 GC 算法 JVM 为此
  • CMake 无法确定 C++ 的链接器语言

    我正在尝试使用 Visual Studio 2010 和 Cygwin 在 Windows 7 x64 上运行 cmake hello world 程序 但似乎都无法工作 我的目录结构如下 HelloWorld CMakeLists txt
  • Webpack怎么直接引入webpack.config.babel.js呢?

    我是一个相当大的 ReactJS 项目的新手 在顶层 它确实not有平常的webpack config js 但只有一个 webpack config babel js 这个确实被使用了 我可以在运行时验证 gt webpack 被调用 通
  • CSS 样式不适用于 ID

    我在用asp net C 对于我的项目 目前我有一个default aspx页面和母版页 default aspx页面有很多控件 并且对于每个控件我都定义了class and id 我面临的问题是当我申请时style使用特定的控件class
  • 具有 NFS 持久卷的 Kubernetes 有状态集

    我有一个kubernetes集群 我有一个简单的部署mongodb with NFS持久卷设置 它工作得很好 但是由于像数据库这样的资源stateful我想用Statefulset为了mongodb 但现在的问题是 当我查看文档时 stat
  • 添加误差线以在 R 中的绘图上显示标准差

    对于每个X value 我计算了平均值Y 值和标准差 sd 每个 Y 值 x 1 5 y c 1 1 1 5 2 9 3 8 5 2 sd c 0 1 0 3 0 2 0 2 0 4 plot x y 如何使用标准差向绘图的每个数据点添加误
  • 无法从 pod 容器内部访问 kubernetes api

    我已经创建了 hashcorpVault 部署并配置了 kubernetes auth Vault 容器从 pod 内部调用 kubernetes api 进行 k8s 身份验证 该调用失败并显示 500 错误代码 连接被拒绝 我正在使用
  • 在表中启用和禁用 td

    td td
  • 如何使用 MUnit 在 Mule Flow 中模拟 Java 组件

    我正在尝试使用 MUnit 对我的一个子流程进行单元测试 我需要模拟一个自定义 Java 组件 但我无法这样做 我的子流程如下