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
Raghu PedabbaRaghu Pedabba 

Not sure how to create a Test Class for my Web Services.

Hi Gurus,

I have created a Web Service for Email Validation , its work fine. But not sure how to write  a test class for this.

//Generated by wsdl2apex

public class pca_Email_validation {
    public class EmailValidation_Batch_Validate_v1_20_Results {
        public String Status;
        public String EmailAddress;
        public String Account;
        public String Domain;
        public Boolean IsDisposible;
        public Boolean IsSystemMailbox;
        private String[] Status_type_info = new String[]{'Status','http://services.postcodeanywhere.co.uk/',null,'0','1','false'};
        private String[] EmailAddress_type_info = new String[]{'EmailAddress','http://services.postcodeanywhere.co.uk/',null,'0','1','false'};
        private String[] Account_type_info = new String[]{'Account','http://services.postcodeanywhere.co.uk/',null,'0','1','false'};
        private String[] Domain_type_info = new String[]{'Domain','http://services.postcodeanywhere.co.uk/',null,'0','1','false'};
        private String[] IsDisposible_type_info = new String[]{'IsDisposible','http://services.postcodeanywhere.co.uk/',null,'1','1','false'};
        private String[] IsSystemMailbox_type_info = new String[]{'IsSystemMailbox','http://services.postcodeanywhere.co.uk/',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://services.postcodeanywhere.co.uk/','true','false'};
        private String[] field_order_type_info = new String[]{'Status','EmailAddress','Account','Domain','IsDisposible','IsSystemMailbox'};
    }
    public class EmailValidation_Batch_Validate_v1_20_element {
        public String Key;
        public String Emails;
        private String[] Key_type_info = new String[]{'Key','http://services.postcodeanywhere.co.uk/',null,'0','1','false'};
        private String[] Emails_type_info = new String[]{'Emails','http://services.postcodeanywhere.co.uk/',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://services.postcodeanywhere.co.uk/','true','false'};
        private String[] field_order_type_info = new String[]{'Key','Emails'};
    }
    public class EmailValidation_Batch_Validate_v1_20_Response_element {
        public pca_Email_validation.EmailValidation_Batch_Validate_v1_20_ArrayOfResults EmailValidation_Batch_Validate_v1_20_Result;
        private String[] EmailValidation_Batch_Validate_v1_20_Result_type_info = new String[]{'EmailValidation_Batch_Validate_v1_20_Result','http://services.postcodeanywhere.co.uk/',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://services.postcodeanywhere.co.uk/','true','false'};
        private String[] field_order_type_info = new String[]{'EmailValidation_Batch_Validate_v1_20_Result'};
    }
    public class EmailValidation_Batch_Validate_v1_20_ArrayOfResults {
        public pca_Email_validation.EmailValidation_Batch_Validate_v1_20_Results[] EmailValidation_Batch_Validate_v1_20_Results;
        private String[] EmailValidation_Batch_Validate_v1_20_Results_type_info = new String[]{'EmailValidation_Batch_Validate_v1_20_Results','http://services.postcodeanywhere.co.uk/',null,'0','-1','false'};
        private String[] apex_schema_type_info = new String[]{'http://services.postcodeanywhere.co.uk/','true','false'};
        private String[] field_order_type_info = new String[]{'EmailValidation_Batch_Validate_v1_20_Results'};
    }
    public class PostcodeAnywhere_Soap {
        public String endpoint_x = 'https://services.postcodeanywhere.co.uk/EmailValidation/Batch/Validate/v1.20/soapnew.ws';
        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://services.postcodeanywhere.co.uk/', 'pca_Email_validation'};
        public pca_Email_validation.EmailValidation_Batch_Validate_v1_20_ArrayOfResults EmailValidation_Batch_Validate_v1_20(String Key,String Emails) {
            pca_Email_validation.EmailValidation_Batch_Validate_v1_20_element request_x = new pca_Email_validation.EmailValidation_Batch_Validate_v1_20_element();
            request_x.Key = Key;
            request_x.Emails = Emails;
            pca_Email_validation.EmailValidation_Batch_Validate_v1_20_Response_element response_x;
            Map<String, pca_Email_validation.EmailValidation_Batch_Validate_v1_20_Response_element> response_map_x = new Map<String, pca_Email_validation.EmailValidation_Batch_Validate_v1_20_Response_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              'http://services.postcodeanywhere.co.uk/EmailValidation_Batch_Validate_v1_20',
              'http://services.postcodeanywhere.co.uk/',
              'EmailValidation_Batch_Validate_v1_20',
              'http://services.postcodeanywhere.co.uk/',
              'EmailValidation_Batch_Validate_v1_20_Response',
              'pca_Email_validation.EmailValidation_Batch_Validate_v1_20_Response_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.EmailValidation_Batch_Validate_v1_20_Result;
        }
    }
}

Calling this From my Custom Controller

public class DispatcherLeadNew{
    
    public DispatcherLeadNew(ApexPages.StandardController controller) {
        this.controller = controller;
    }
       public PageReference Save_Validate(){
        
       newLead = (Lead) controller.getRecord();     
        
       String Email = newLead.Email;
       String MPhone = newLead.MobilePhone;
       Boolean Va    = newLead.Validate__c;
       System.debug('Value of Boolean ' + Va);
       
           if(Va){
               Email_validation(Email);
            Phone_validation(Mphone);
           }    
         public void Email_Validation(String mail){

        
        
        //pca_Email_validation.EmailValidation_Batch_Validate_v1_20_Results wa;
        String Key = 'XXXX-XXXX-XXXX-XXXX';
        //String Email = newLead.Email;

          if(String.isNotBlank(mail)){  
            pca_Email_validation.PostcodeAnywhere_Soap stub = new  pca_Email_validation.PostcodeAnywhere_Soap();
            pca_Email_validation.EmailValidation_Batch_Validate_v1_20_Results stub2 = new pca_Email_validation.EmailValidation_Batch_Validate_v1_20_Results();
            pca_Email_validation.EmailValidation_Batch_Validate_v1_20_ArrayOfResults stub1 = new pca_Email_validation.EmailValidation_Batch_Validate_v1_20_ArrayOfResults();

                stub1 = stub.EmailValidation_Batch_Validate_v1_20(Key,mail);
                system.debug(stub1.EmailValidation_Batch_Validate_v1_20_Results[0].status);
                String valid = stub1.EmailValidation_Batch_Validate_v1_20_Results[0].status;        
                newLead.valid_email__c = valid;
         }   
       private final ApexPages.StandardController controller;
    }

Please can someone guide me to write a test class for this ?

Thanks,
Raghu Ram





 
Amit Chaudhary 8Amit Chaudhary 8
I will recoment you please check below Trailhead module for API related issue. I hope that will help you how to create mock class for web service class.
1) https://developer.salesforce.com/trailhead/apex_integration_services/apex_integration_rest_callouts
2) https://developer.salesforce.com/trailhead/apex_integration_services/apex_integration_soap_callouts

Let us know if this will help you

Thanks
Amit Chaudhary
Raghu PedabbaRaghu Pedabba
Hi Amit,

Thanks for the reply, I did try that ..but got the below error

System.TypeException: Collection store exception putting pca_Email_validation.EmailValidation_Batch_Validate_v1_20_Results into Map<String,pca_Email_validation.EmailValidation_Batch_Validate_v1_20_Response_element>

Class.WebServiceMockImpl.doInvoke: line 17, column 1
Class.pca_Email_validation.PostcodeAnywhere_Soap.EmailValidation_Batch_Validate_v1_20: line 56, column 1
Class.DispatcherLeadNewTest.pcavalidation: line 67, column 1