使用java在SOAP UI中创建Rest项目

时间:2017-11-21 13:38:43

标签: java soap soapui

我能够创建SOAP项目并执行相关的测试用例。

SOAP Code Snippet:

public String executeSoapTestcase(final SoapUIData soapUIData) throws Exception {
       SoapUI.getSettings().setBoolean(HttpSettings.RESPONSE_COMPRESSION, false);
        final WsdlProject project = new WsdlProject();
        String response = null;

        final WsdlInterface[] ifaceArray = WsdlInterfaceFactory.importWsdl(project, soapUIData.getWsdlPath(), true);
        if (ifaceArray == null) {
            throw new IllegalArgumentException(
                    "inside SoapUIService.executeTestcase Binding  not found in the specified WSDL");
        }
        final WsdlInterface wsdlInterface = ifaceArray[0];
        Operation[] wsdlOperations = wsdlInterface.getAllOperations();
        if (wsdlOperations == null) {
            throw new IllegalArgumentException(
                    "inside SoapUIService.executeTestcase Operations  not found in the specified WSDL");
        }
        wsdlInterface.changeEndpoint(wsdlInterface.getEndpoints()[0], soapUIData.getEndpoint());
        final WsdlOperation operation = (WsdlOperation) wsdlOperations[0];
        final WsdlRequest request = operation.addNewRequest("addRequest");
        request.setRequestContent(soapUIData.getXmlRequest());
        final WsdlSubmit<?> wsdlSubmit = (WsdlSubmit<?>) request.submit(new WsdlSubmitContext(request), false);
        final WsdlResponse wsdlResponse = (WsdlResponse) wsdlSubmit.getResponse();
        log.info("inside utm-soapui-service SoapUIService.executeTestcase Submit status : " + wsdlSubmit.getStatus());
        if (wsdlResponse != null) {
            response = wsdlResponse.getContentAsString();
        }

        return response;
    }

我想为REST项目做同样的事情:我想使用wadl创建Rest Project并执行测试用例。

1 个答案:

答案 0 :(得分:0)

 public void executeRestTestcase() throws Exception {       

    final WsdlProject project = new WsdlProject();
    final RestServiceBuilder serviceBuilder = new RestServiceBuilder();
    serviceBuilder.createRestService(project,"Your Rest URL");
    final StringToStringMap headers = new StringToStringMap();
    headers.put("Authorization", "Basic **********");
    headers.put("Accept", "application/json");
    final RestRequest request =
            (RestRequest) project.getInterfaceList().get(0).getOperationList().get(0).getRequestList().get(0);
    request.setRequestHeaders(headers);
    final Submit submit = (Submit) request.submit(new WsdlSubmitContext(request), false);
    final Response response = submit.getResponse();
    String responseContent = response.getContentAsString();
    log.info(responseContent);


}