用Java

时间:2016-02-04 05:32:59

标签: java automated-tests soapui

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

1 个答案:

答案 0 :(得分: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);
}