2016-02-04 44 views
1

我正在使用Java的SOAP UI API。我創建了我的SOAP項目並添加了測試套件和必需的測試用例和測試步驟。我只是想知道是否有任何方式獲得XML格式的測試響應?因爲我想在我的測試在Java中顯示SOAPUI TestRunner響應

import com.eviware.soapui.impl.wsdl.WsdlProject; 
import com.eviware.soapui.model.iface.MessageExchange; 
import com.eviware.soapui.model.support.PropertiesMap; 
import com.eviware.soapui.model.testsuite.TestCase; 
import com.eviware.soapui.model.testsuite.TestRunner; 
import com.eviware.soapui.model.testsuite.TestSuite; 
import org.junit.Assert; 
import java.util.List; 
public class SoapUITest 
{ 
    public final static void main(String [] args) throws Exception { 

    WsdlProject project = new WsdlProject("C:\\WebService\\WebServiceTest\\src\\main\\java\\weather.xml"); 
    List<TestSuite> testSuites = project.getTestSuiteList(); 
    for (TestSuite testSuite: testSuites) 
    { 
     System.out.println("Running Test Suite: "+ testSuite.getName()); 
     List<TestCase> testCases = testSuite.getTestCaseList(); 
     for(TestCase testCase:testCases) 
     { 
      System.out.println("Running Test Case: " + testCase.getName()); 
      TestRunner testRunner = testCase.run(new PropertiesMap(), false); 
      Assert.assertEquals(TestRunner.Status.FINISHED,testRunner.getStatus()); 

      //Exception in the below line 
      //System.out.println(((MessageExchange)testRunner).getResponseContent()); 
     } 
    } 
    System.out.print("Testing finished successfully"); 
    } 
} 

我加入這個代碼System.out.println(((MessageExchange)testRunner).getResponseContent())但明顯的原因,我得到以下異常進一步使用這些數據。不知道我是否使用正確的方法。

Exception in thread "main" java.lang.ClassCastException: com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner cannot be cast to com.eviware.soapui.model.iface.MessageExchange 
at VMSSoapUI.main(SoapUITest.java:36) 
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

是否有人已經實現了這種方式發送請求並從SOAPUITestRunner獲取XML格式的響應?感謝你的幫助。

+0

你應該運行'測試步驟'以期望得到響應。但是,'test case'在你的情況下運行。因此,循環測試步驟。 – Rao

+0

你能否給我提供代碼片段。謝謝。 – vkrams

+0

代碼片段?循環通過測試步驟?或訪問響應? – Rao

回答

3

下面runStepAndGetResponseContent方法將要運行的測試步驟,並獲得響應,串wsdl request test step類型,我相信,你是要找的人。您可以根據需要爲測試步驟類型的其他實例添加條件。

public String runStepAndGetResponseContent(WsdlTestCaseRunner runner, TestStep testStep) { 
     TestStepResult result = runner.runTestStep(testStep); 
     if (result instanceof WsdlTestRequestStepResult) { 
      return ((WsdlTestRequestStepResult) result).getResponse().getContentAsString(); 
     } 
     return null; 
} 

隨着上述新方法,下面的你的代碼線需要與下面的代碼來替換:

現有:此意願:
TestRunner testRunner = testCase.run(new PropertiesMap(), false);

替換爲循環通過測試用例的測試步驟。

WsdlTestCaseRunner runner = new WsdlTestCaseRunner((WsdlTestCase) testCase, new StringToObjectMap(testCase.getProperties())); 
for(TestStep testStep: testSteps){ 
     //calling the above get method here 
     String response = runStepAndGetResponseContent(runner, testStep); 
     System.out.print("Received response is :" + response); 
} 
+0

謝謝你,感謝你的努力。其工作順利。我得到了XML響應,我可以做進一步的處理。 – vkrams