mule测试中面临的问题

mule测试中面临的问题,mule,Mule,我面临着骡子测试的问题。我只是尝试一下简单的功能测试示例。 下面是我的流程 <flow name="muletestFlow1" doc:name="muletestFlow1"> <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="test" doc:name="HTTP"/> <response

我面临着骡子测试的问题。我只是尝试一下简单的功能测试示例。 下面是我的流程

 <flow name="muletestFlow1" doc:name="muletestFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="test" doc:name="HTTP"/>
        <response>
            <object-to-string-transformer />
        </response>
        <component class="com.org.Square" doc:name="Square"/>
        <component class="com.org.Neg doc:name="Neg"/>
    </flow>
若我以junit测试的形式运行上述类,那个么会出现以下错误

java.lang.AssertionError
    at org.junit.Assert.fail(Assert.java:92)
    at org.junit.Assert.assertTrue(Assert.java:43)
    at org.junit.Assert.assertTrue(Assert.java:54)
    at com.test.TestEx.testSquareAddInverse(TestEx.java:25)
    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:622)
    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.rules.TestWatcher$1.evaluate(TestWatcher.java:46)
    at org.junit.internal.runners.statements.FailOnTimeout$1.run(FailOnTimeout.java:28)
有什么解决办法请帮助我。 如果我评论这句话

assertTrue(reply.getPayload() instanceof String);
然后得到以下错误

java.lang.ClassCastException: org.mule.transport.http.ReleasingInputStream cannot be cast to java.lang.String
这个代码有什么问题?我怎样才能解决这个问题。请帮帮我


由于您通过http协议调用流,这会隐式地转换负载类型,因此请提前感谢。因此,单元测试中的应答将不包含字符串,而是一个InputStream。这是预期的,因此您应该测试InputStream而不是字符串。要获取字符串形式的值,可以使用使用注册的转换器将有效负载转换为字符串的方法

    @Test
    public void testGetNegaiveNine() throws Exception {
        MuleClient client = muleContext.getClient();
        MuleMessage reply = client.send ("http://localhost:8081/test", "3", null, 5000);

        assertNotNull(reply);
        assertNotNull(reply.getPayload());
        assertTrue(reply.getPayload() instanceof InputStream);
        String result = reply.getPayload(String.class);
        assertEquals("-9", result);
    }
在流程中得到的异常 在那之后

"DefaultJavaComponent{muletestFlow1.component.3418143}. Message payload is of type: ContentLengthInputStream"
是因为组件无法处理流中的消息所具有的有效负载。因为您正在向流中发送HTTP POST,所以传入消息的有效负载是ContenLengthInputStream,您需要一个字符串

因此,您必须将有效负载转换为字符串,方法是在组件之前向字符串转换器添加一个对象。整个流程将是这样的

<flow name="muletestFlow1" doc:name="muletestFlow1">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="test" doc:name="HTTP"/>
    <response>
        <!-- Ensure that the response is a String -->
        <object-to-string-transformer />
    </response>
    <!-- Ensure that the incoming request is a String -->
    <object-to-string-transformer />
    <component class="com.org.Square" doc:name="Square"/>
    <component class="com.org.Neg doc:name="Neg"/>
</flow>


谢谢你的回复。我根据你的建议改变了。现在我得到了这个错误“导致异常的组件是:DefaultJavaComponent{muletestFlow1.Component.3418143}。消息负载的类型是:ContentLengthInputStream”我尝试使用vm端点执行此操作,然后可以成功运行。但是如何使用http端点执行此操作请提供帮助,因为我在通过http调用流时回答了负载是一个InputStream(确切地说是ContenLengthInputStream)。这是预期的行为,因此您应该对此进行测试。此外,我还希望将有效负载显式转换为字符串的语句也会失败。稍后当我在电脑前时,我会用一个工作测试班来更新我的答案。同时,请随时接受我的回答;-)更新答案以反映新添加的信息。
<flow name="muletestFlow1" doc:name="muletestFlow1">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="test" doc:name="HTTP"/>
    <response>
        <!-- Ensure that the response is a String -->
        <object-to-string-transformer />
    </response>
    <!-- Ensure that the incoming request is a String -->
    <object-to-string-transformer />
    <component class="com.org.Square" doc:name="Square"/>
    <component class="com.org.Neg doc:name="Neg"/>
</flow>