在 Java 中显示 SOAPUI TestRunner 响应

Display SOAPUI TestRunner Response in Java

我正在使用来自 Java 的 SOAP UI API。我已经创建了我的 SOAP 项目并添加了一个测试套件和所需的测试用例和测试步骤。我只是想知道有什么方法可以得到 XML 格式的测试响应吗?因为我想在我的测试中进一步使用这些数据

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 格式获取响应的方式?感谢您的帮助。

下面runStepAndGetResponseContent 方法将运行测试步骤并得到响应作为字符串 for wsdl request test step type,我相信,这就是你要找的。您可以根据需要为其他测试步骤类型实例添加条件。

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);
}