function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
EdenEden 

Multiple response for WebServiceMock

How do I set multiple responses for different call outs in this class that implements WebServiceMock?

@isTest
global class PuchaseWSMock_IsTest implements WebServiceMock {
    global void doInvoke(Object stub,
                        Object request,
                        Map<String, Object> response,
                        String endpoint,
                        String soapAction,
                        String requestName,
                        String responseNS,
                        String responseName,
                        String responseType) {
       
        //set response for web service method 1                   
        PurchaseWS.ProgramInfoResponse responseElement = new PurchaseWS.ProgramInfoResponse();
        responseElement.AgreementID = 344;
                            
        response.put('response_x', responseElement);

       //set response for web service method2?
    }
}

 


Test method:
static testMethod void myTest() {
       Test.setMock(WebServiceMock.class, new PurchaseWSMock_IsTest());
       
     Test.startTest();
      //call web service method 1
      //call web service method 2
     Test.startTest();
}

MTBRiderMTBRider
I had the same situation recently, but a different problem with it.  Here is my post: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AhjlIAC

I
f  you go to the last post, you can see how I implemented multiple mock responses.  Hopefully this helps you, if not let me know.
Jhon Cortes 7Jhon Cortes 7
You could create a constructor for example:
 
@isTest
global class PuchaseWSMock_IsTest implements WebServiceMock {

    public String type;

    global PuchaseWSMock_IsTest(final String type){
             this.type = type;
    }

    global void doInvoke(Object stub,
                        Object request,
                        Map<String, Object> response,
                        String endpoint,
                        String soapAction,
                        String requestName,
                        String responseNS,
                        String responseName,
                        String responseType) {
       
        if(type == 'A'){
              //set response for web service method 1                   
              PurchaseWS.ProgramInfoResponse responseElementA = new PurchaseWS.ProgramInfoResponse();
              responseElement.AgreementID = 344;
              response.put('response_x', responseElementA);
        }


       //set response for web service method2?
         if(type == 'B'){
              //set response for web service method 2          
              PurchaseWS.ProgramInfoResponse responseElementB = new PurchaseWS.ProgramInfoResponse();
              responseElement.AgreementID = 100;
              response.put('response_x', responseElementB);
        }
    }
}
 

Test method:
static testMethod void myTestA() {
       Test.setMock(WebServiceMock.class, new PurchaseWSMock_IsTest('A'));
       
     Test.startTest();
      //call web service method 1
      Test.startTest();
}

static testMethod void myTestB() {
       Test.setMock(WebServiceMock.class, new PurchaseWSMock_IsTest('B'));
       
     Test.startTest();
      //call web service method 2
     Test.startTest();
}



 
bhavesh shahubhavesh shahu
Try do debug your method name .Check this code. 
@isTest
global class WebServiceGetReferenceMockImpl implements WebServiceMock {
    global void doInvoke(
        Object stub,
        Object request,
        Map<String, Object> response,
        String endpoint,
        String soapAction,
        String requestName,
        String responseNS,
        String responseName,
        String responseType) {
         
            System.debug(requestName);
  
            // Method 1
            if(requestName == 'method1'){
           // code
            }
           // Method 2
            if(requestName == 'method2'){
           // code
            }
}