我正在使用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
方法将运行测试步骤,并以字符串形式表示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);
}