显示在Java中SOAPUI的TestRunner响应

显示在Java中SOAPUI的TestRunner响应

本文介绍了显示在Java中SOAPUI的TestRunner响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用从Java SOAP UI API。我创建了我的SOAP项目,并增加了测试套件和必需的测试用例和测试步骤。我只是想知道有没有什么办法让测试的XML格式的反应呢?因为我想在我的测试中进一步使用这些数据。

I am using SOAP UI API from Java. I have created my SOAP Project and added a Test Suite and Required Test Cases and Test Steps. I am just wondering is there any way getting the response of test in XML format? Because I want to use this data further in my tests

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

我加入这个code 的System.out.println(((MessageExchange)的TestRunner).getResponseContent())但显而易见的原因,我得到以下例外。不知道我是否使用正确的方法。

I have added this code System.out.println(((MessageExchange)testRunner).getResponseContent()) But for obvious reason I am getting the below Exception. Not sure whether I am using the right method.

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格式的响应的这种方式?鸭preciate你的帮助。

Did some one already implemented this way of sending the request and getting the response in XML format from SOAPUITestRunner? Appreciate your help.

推荐答案

runStepAndGetResponseContent 方法的运行测试步骤和取得的响应作为字符串的为 WSDL要求测试步骤类型,我相信,就是你要找的人。你可以根据需要为测试步骤类型的其他实例添加条件。

Below runStepAndGetResponseContent method will run the test step and get the response as string for wsdl request test step type, which I believe, is the one you looking for. And you may add conditions for other instances of test step types as needed.

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

随着上述新方法,下面你的code线需要与低于code被替换:

Along with the above new method, below line of code of yours needs to be replaced with below code:

现有结果
的TestRunner的TestRunner = testCase.run(新PropertiesMap(),FALSE);

替换:这将环通一个测试用例的测试步骤

Replace with: this will loop thru the test steps of a test case.

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

这篇关于显示在Java中SOAPUI的TestRunner响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 15:28