2014-12-31 30 views
0

我正面臨着騾子測試的問題。我只是嘗試一下簡單的功能測試示例。 以下是我的流程騾子測試中面臨的問題

<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> 

和下面是我的測試類

@Test 
    public void testSquareAddInverse() 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 String); 
     String result = (String)reply.getPayload(); 
     assertEquals("-9", result); 

如果我運行上面的類作爲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 

這段代碼有什麼問題?我怎麼解決這個問題。請幫我

在此先感謝

回答

2

既然你打電話通過HTTP協議,這隱含轉換有效載荷類型的流量。所以單元測試中的回覆將不包含一個String,而是一個InputStream。這是預期的,所以你應該測試一個InputStream而不是String。要將值作爲字符串獲取,您可以使用方法MuleMessage#getPayload(java.lang.Class),該方法將使用註冊的變換器將有效負載轉換爲字符串。

@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> 
+0

感謝您的答覆。我根據您的建議進行了更改。現在我得到了這個錯誤「導致異常的組件是:DefaultJavaComponent {muletestFlow1.component.3418143}。消息有效載荷的類型爲:ContentLengthInputStream」 – user3855589

+0

我試過這個使用VM端點,然後我可以成功運行。但我怎麼能使用http端點請幫忙 – user3855589

+0

當我在HTTP上調用流時是一個InputStream(準確地說ContenLengthInputStream)時,我回答了有效載荷。這是預期的行爲,所以你應該測試。此外,我期望您明確將有效負載轉換爲字符串的語句也會失敗。稍後當我在電腦前時,我會更新自己的答案。 –