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
Veena GopalVeena Gopal 

apex soap callouts

In the Developer Intermediate ----> Apex Integration Services ------>apex soap callouts, 
a test example is given class name is AwesomeCalculatorTest.
@isTest
private class AwesomeCalculatorTest {
    @isTest static void testCallout() {              
        // This causes a fake response to be generated
        Test.setMock(WebServiceMock.class, new CalculatorCalloutMock());
        // Call the method that invokes a callout
        Double x = 1.0;
        Double y = 2.0;
        Double result = AwesomeCalculator.add(x, y);
        // Verify that a fake result is returned
        System.assertEquals(3.0, result); 
    }
}
This one tests the add functionality of the generated wsdl class.
The below is the test class for mock callout and below is the wsdl class
@isTest
global class CalculatorCalloutMock 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) {
        // start - specify the response you want to send
        calculatorServices.doAddResponse response_x = 
            new calculatorServices.doAddResponse();
        response_x.return_x = 3.0;
        // end
        response.put('response_x', response_x); 
   }
}
 
//Generated by wsdl2apex

public class calculatorServices {
    public class doDivideResponse {
        public Double return_x;
        private String[] return_x_type_info = new String[]{'return','http://calculator.services/',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://calculator.services/','false','false'};
        private String[] field_order_type_info = new String[]{'return_x'};
    }
    public class doMultiply {
        public Double arg0;
        public Double arg1;
        private String[] arg0_type_info = new String[]{'arg0','http://calculator.services/',null,'0','1','false'};
        private String[] arg1_type_info = new String[]{'arg1','http://calculator.services/',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://calculator.services/','false','false'};
        private String[] field_order_type_info = new String[]{'arg0','arg1'};
    }
    public class doAdd {
        public Double arg0;
        public Double arg1;
        private String[] arg0_type_info = new String[]{'arg0','http://calculator.services/',null,'0','1','false'};
        private String[] arg1_type_info = new String[]{'arg1','http://calculator.services/',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://calculator.services/','false','false'};
        private String[] field_order_type_info = new String[]{'arg0','arg1'};
    }
    public class doAddResponse {
        public Double return_x;
        private String[] return_x_type_info = new String[]{'return','http://calculator.services/',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://calculator.services/','false','false'};
        private String[] field_order_type_info = new String[]{'return_x'};
    }
    public class doDivide {
        public Double arg0;
        public Double arg1;
        private String[] arg0_type_info = new String[]{'arg0','http://calculator.services/',null,'0','1','false'};
        private String[] arg1_type_info = new String[]{'arg1','http://calculator.services/',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://calculator.services/','false','false'};
        private String[] field_order_type_info = new String[]{'arg0','arg1'};
    }
    public class doSubtract {
        public Double arg0;
        public Double arg1;
        private String[] arg0_type_info = new String[]{'arg0','http://calculator.services/',null,'0','1','false'};
        private String[] arg1_type_info = new String[]{'arg1','http://calculator.services/',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://calculator.services/','false','false'};
        private String[] field_order_type_info = new String[]{'arg0','arg1'};
    }
    public class doSubtractResponse {
        public Double return_x;
        private String[] return_x_type_info = new String[]{'return','http://calculator.services/',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://calculator.services/','false','false'};
        private String[] field_order_type_info = new String[]{'return_x'};
    }
    public class doMultiplyResponse {
        public Double return_x;
        private String[] return_x_type_info = new String[]{'return','http://calculator.services/',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://calculator.services/','false','false'};
        private String[] field_order_type_info = new String[]{'return_x'};
    }
    public class CalculatorImplPort {
        public String endpoint_x = 'https://th-apex-soap-service.herokuapp.com/service/calculator';
        public Map<String,String> inputHttpHeaders_x;
        public Map<String,String> outputHttpHeaders_x;
        public String clientCertName_x;
        public String clientCert_x;
        public String clientCertPasswd_x;
        public Integer timeout_x;
        private String[] ns_map_type_info = new String[]{'http://calculator.services/', 'calculatorServices'};
        public Double doDivide(Double arg0,Double arg1) {
            calculatorServices.doDivide request_x = new calculatorServices.doDivide();
            request_x.arg0 = arg0;
            request_x.arg1 = arg1;
            calculatorServices.doDivideResponse response_x;
            Map<String, calculatorServices.doDivideResponse> response_map_x = new Map<String, calculatorServices.doDivideResponse>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'http://calculator.services/',
              'doDivide',
              'http://calculator.services/',
              'doDivideResponse',
              'calculatorServices.doDivideResponse'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.return_x;
        }
        public Double doSubtract(Double arg0,Double arg1) {
            calculatorServices.doSubtract request_x = new calculatorServices.doSubtract();
            request_x.arg0 = arg0;
            request_x.arg1 = arg1;
            calculatorServices.doSubtractResponse response_x;
            Map<String, calculatorServices.doSubtractResponse> response_map_x = new Map<String, calculatorServices.doSubtractResponse>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'http://calculator.services/',
              'doSubtract',
              'http://calculator.services/',
              'doSubtractResponse',
              'calculatorServices.doSubtractResponse'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.return_x;
        }
        public Double doMultiply(Double arg0,Double arg1) {
            calculatorServices.doMultiply request_x = new calculatorServices.doMultiply();
            request_x.arg0 = arg0;
            request_x.arg1 = arg1;
            calculatorServices.doMultiplyResponse response_x;
            Map<String, calculatorServices.doMultiplyResponse> response_map_x = new Map<String, calculatorServices.doMultiplyResponse>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'http://calculator.services/',
              'doMultiply',
              'http://calculator.services/',
              'doMultiplyResponse',
              'calculatorServices.doMultiplyResponse'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.return_x;
        }
        public Double doAdd(Double arg0,Double arg1) {
            calculatorServices.doAdd request_x = new calculatorServices.doAdd();
            request_x.arg0 = arg0;
            request_x.arg1 = arg1;
            calculatorServices.doAddResponse response_x;
            Map<String, calculatorServices.doAddResponse> response_map_x = new Map<String, calculatorServices.doAddResponse>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'http://calculator.services/',
              'doAdd',
              'http://calculator.services/',
              'doAddResponse',
              'calculatorServices.doAddResponse'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.return_x;
        }
    }
}

I want to test all the add, divide, multiply, subtract in one go for which i have to change the class CalculatorCalloutMock and AwesomeCalculatorTest. When i make changes in the CalculatorCalloutMock class, it gives me error response_x and return_x duplicate since it is the same variable used in all the doResponse. 
Please help how i can test all functionality in one test class to achieve 100% coverage.
 
Özhan GüneşÖzhan Güneş
Hi there! I tried to solve this problem too. First of all, in the CalculatorCalloutMock class, we need to check the "requestName" first. In accord with the  "requestName", I created a response instance. After that, I assigned the result of the transaction to response_x.return_x variable.
 
@istest
global with sharing class CalculatorCalloutMock 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) {
        // start - specify the response you want to send


        if(requestName == 'doAdd'){
            calculatorServices.doAddResponse response_x =
            new calculatorServices.doAddResponse();
            response_x.return_x = 3.0;
            response.put('response_x', response_x);
        }else if (requestName == 'doSubtract'){
            calculatorServices.doSubtractResponse response_x =
            new calculatorServices.doSubtractResponse();
            response_x.return_x = 1.0;
            response.put('response_x', response_x);
        }else if (requestName == 'doMultiply'){
            calculatorServices.doMultiplyResponse response_x =
            new calculatorServices.doMultiplyResponse();
            response_x.return_x = 2.0;
            response.put('response_x', response_x);
        }else if (requestName == 'doDivide'){
            calculatorServices.doDivideResponse response_x =
            new calculatorServices.doDivideResponse();
            response_x.return_x = 2.0;
            response.put('response_x', response_x);
        }
    }
}
After completing the mock class, I wrote the  AwesomeCalculator and AwesomeCalculatorTest as follows.
 
public with sharing class AwesomeCalculator {
    public static Double add(Double x, Double y) {
        calculatorServices.CalculatorImplPort calculator = 
            new calculatorServices.CalculatorImplPort();
        return calculator.doAdd(x,y);
    }

    public static Double multiply(Double x, Double y) {
        calculatorServices.CalculatorImplPort calculator = 
            new calculatorServices.CalculatorImplPort();
        return calculator.doMultiply(x,y);
    }

    public static Double subtract(Double x, Double y) {
        calculatorServices.CalculatorImplPort calculator = 
            new calculatorServices.CalculatorImplPort();
        return calculator.doSubtract(x,y);
    }

    public static Double divide(Double x, Double y) {
        calculatorServices.CalculatorImplPort calculator = 
            new calculatorServices.CalculatorImplPort();
        return calculator.doDivide(x,y);
    }
}
 
@istest
public with sharing class AwesomeCalculatorTest {
    @istest
    static void testCallout(){
        //This causes a fake response to be generated
        Test.setMock(WebServiceMock.class, new CalculatorCalloutMock());
        //Call the method that invokes a callout
        Double x = 2.0;
        Double y = 1.0;
        Double result = AwesomeCalculator.add(x,y);
        System.assertEquals(3.0, result);
        Double result1 = AwesomeCalculator.multiply(x,y);
        System.assertEquals(2.0, result1);
        Double result2 = AwesomeCalculator.subtract(x,y);
        System.assertEquals(1.0, result2);
        Double result3 = AwesomeCalculator.divide(x,y);
        System.assertEquals(2.0, result3);
    }
}