java WebServiceMockImpl,单个系统范围的WebService Mock,带有用法示例。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java WebServiceMockImpl,单个系统范围的WebService Mock,带有用法示例。相关的知识,希望对你有一定的参考价值。

/**
* ===============================================================================================
*   Version     		Date            								Comment
* ===============================================================================================
*   Initial     6-Jan-2017     Generic class for web service mock
*/

@isTest
public class WebServiceMockImpl implements WebServiceMock {
    public static Map<String, Object> returnToMe;
    
    public void doInvoke(
        Object stub,
        Object request,
        Map<String, Object> response,
        String endpoint,
        String soapAction,
        String requestName,
        String responseNS,
        String responseName,
        String responseType) {
            response.putAll(returnToMe);
        }
}
@isTest
private class STCElectronicDataInterchangeWSTest {

    @isTest static void test_validateWebstoreOrder() {

        List<STCElectronicDataInterchangeWS.lineItem> listLineItem = new List<STCElectronicDataInterchangeWS.lineItem>();
        STCElectronicDataInterchangeWS.lineItem objLineItem = new STCElectronicDataInterchangeWS.lineItem();
        objLineItem.language = 'testLang';
        objLineItem.lineNumber = 1;
        objLineItem.quantity = 1;
        objLineItem.sku = 'testSku';

        listLineItem.add(objLineItem);
        
        STCElectronicDataInterchangeWS.wsResponse objWsResponse = new STCElectronicDataInterchangeWS.wsResponse();
        objWsResponse.error = false;
        
        STCElectronicDataInterchangeWS.validateWebstoreOrderResponse objWebstoreOrderResponse = new STCElectronicDataInterchangeWS.validateWebstoreOrderResponse();
        objWebstoreOrderResponse.return_x = objWsResponse;
        
        WebServiceMockImpl.returnToMe = new Map<String, STCElectronicDataInterchangeWS.validateWebstoreOrderResponse>();
        WebServiceMockImpl.returnToMe.put('response_x', objWebstoreOrderResponse);
        
        System.Test.setMock(WebServiceMock.class, new WebServiceMockImpl());

        System.Test.startTest();
        STCElectronicDataInterchangeWS.STCElectronicDataInterchangeWSPort objSTCElectronicDataInterchangeWSPort = new STCElectronicDataInterchangeWS.STCElectronicDataInterchangeWSPort();
        objWsResponse = objSTCElectronicDataInterchangeWSPort.validateWebstoreOrder('TestPassphrase',listLineItem);
        System.Test.stopTest();
        String strResponse = 'wsResponse:[apex_schema_type_info=(http://service.storagecraft.com/, false, false), error=false, errorMessages=null, errorMessages_type_info=(errorMessages, http://service.storagecraft.com/, null, 0, -1, true), error_type_info=(error, http://service.storagecraft.com/, null, 1, 1, false), field_order_type_info=(error, errorMessages)]';
        System.assertEquals(strResponse,String.valueOf(objWsResponse));
    }
}
@isTest
global class HttpCalloutMockImpl implements HttpCalloutMock{

    private String bodyOnly;
    private HTTPResponse fullResponse;
    
    global HttpCalloutMockImpl(String responseBody){
        this.bodyOnly = responseBody;
    }
    
    global HttpCalloutMockImpl(HTTPResponse resp){
        this.fullResponse = resp;
    }
    
    global HTTPResponse respond(HTTPRequest req){
        if(fullResponse != null){
            return fullResponse;
        } else if(bodyOnly != null){
            HTTPResponse r = new HTTPResponse();
            r.setBody(bodyOnly);
            return r;
        } // else
        return new HTTPResponse();
    }
}

以上是关于java WebServiceMockImpl,单个系统范围的WebService Mock,带有用法示例。的主要内容,如果未能解决你的问题,请参考以下文章

java如何获取单选按钮RadioButton的值?

java怎么实现单实例运行

java单选按钮传值

在java中性别是单选按钮,如何把性别添加到数据库中

java中如何判断是哪个单选框被选中了?

Java Regex:查找带转义的单引号文本