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
Hitesh AlgamwarHitesh Algamwar 

I have written test class for rest resource class of Post method where i am facing error.

My code is :- 

@RestResource(URLMapping='/SendEmailForOTP/*')
global class WordpressIntegrationForEmail {
    
    @HTTPPOST
    global static FinalReturnWrapper SendEmailMsg(string email , string otp,string mob,boolean send_otp_if_exists)
    {
        
        FinalReturnWrapper returnResponse = new FinalReturnWrapper(); 
        cls_data objData = new cls_data();
        
        Integer otpSize = otp.length();
        if(otpSize == 5)
        {
            
            list<contact> conList = [Select id,Do_Not_Mail__c,Email_Verification_Status__c, Email_Verification_Status_Date__c,LastName from contact where email =: email Limit 1];
            
            if(conList.size() > 0 )  // If the email id is exist then ...
            {
                objData.contact_exists = true;
                
                for(contact c : conList)
                {
                    objData.contact_id =c.id;
                    objData.name=c.LastName;
                    if(c.Do_Not_Mail__c == false && c.Email_Verification_Status__c == null && c.Email_Verification_Status_Date__c == null)
                    {
                        
                        OtpViaEmail.sendEmail(email,otp);  //Sending Email 
                        objData.otp_sms_sent = true; 
                        objData.otp_email_sent = true;
                        
                    }else 
                    {
                        objData.bounced_email=true;
                    }
                    
                }
                returnResponse.obj = objData;
                System.debug('returnResponse:'+returnResponse);
                return returnResponse;
                
            }
            else    // If the email id not exist then...
            {
                if(send_otp_if_exists==false)
                {
                    objData.contact_exists = false;
                    objData.contact_id =null;
                    OtpViaEmail.sendEmail(email,otp);    //Sending Email 
                    objData.otp_sms_sent = true; 
                    objData.otp_email_sent = true;
                    returnResponse.obj =objData;
                    System.debug('returnResponse:'+returnResponse);
                    return returnResponse;
                }
                
                objData.contact_exists = null;
                objData.contact_id =null;
                objData.otp_sms_sent = false; 
                objData.otp_email_sent = false;
                objData.name=null;
                returnResponse.obj = objData;
                System.debug('returnResponse:'+returnResponse);
                return returnResponse;         
                
            }
            
        }
        objData.contact_exists = null;
        objData.contact_id =null;
        objData.otp_sms_sent = false; 
        objData.otp_email_sent = false;
        objData.name=null;
        returnResponse.obj = objData;
        System.debug('returnResponse:'+returnResponse);
        return returnResponse; 
        
        
        
    }
    
    global class FinalReturnWrapper {
        
        public cls_data obj ;
        
        
    }
    public class cls_data {
        
        public boolean contact_exists;
        public boolean otp_sms_sent;
        public boolean otp_email_sent;
        public string contact_id;
        public boolean bounced_email;     
        public string name;
    }
    
}
__________________________________
ANd here is the test class :- 

@IsTest
public class WordpressIntegrationForEmailTest {
    
    
    public static testMethod void  testPostRestService(){
        
        
        contact c = new contact ();
        c.LastName = 'Pav';
        c.Email = 'pavan@intuitiolabs.com';
        c.Do_Not_Mail__c=false;
        c.Email_Verification_Status__c=null;
        c.Email_Verification_Status_Date__c=null;
        insert c;
        
        Test.startTest();        
        
        string email = 'pavan1@intuitiolabs.com';
        string otp='11223';
        string mob='987654321';
        boolean send_otp_if_exists = false;
        
        
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        
        req.requestURI = '/services/apexrest/WordpressIntegrationForEmail';  //Request URL
        req.httpMethod = 'POST';//HTTP Request Type
        req.requestBody = Blob.valueof('TestMsg');
        req.addParameter(name, value)
        RestContext.request = req;
        RestContext.response= res;
        
        WordpressIntegrationForEmail.SendEmailMsg(email,otp,mob,send_otp_if_exists);
        
        Test.stopTest();
    }
    
}

The error I am facing is below :- System.NullPointerException: Argument cannot be null.
Abdul KhatriAbdul Khatri
Hi Hitesh,

Can you provide more detail about the Error? like the line number the class etc.

I guess you are also calling another class by the name OtpViaEmail, most likely it may be from there but unless you provide the complete error detail, won't be able to able to help.

You can also make one correction in your above main class here
if(conList.size() > 0 )
instead write it like this
if(conList != null && conList.size() > 0 )

Also in your test class, I see 
req.addParameter(name, value)
where is name and value is defined?

Regards
Hitesh AlgamwarHitesh Algamwar

Hi Abdul Khatri 

I have change my class to below :- 

 

@RestResource(URLMapping='/SendEmailForOTP/*')
global class WordpressIntegrationForEmail {
    
    @HTTPPOST
    global static FinalReturnWrapper SendEmailMsg(string email , string otp,string mob,boolean send_otp_if_exists)
    {
        
        FinalReturnWrapper returnResponse = new FinalReturnWrapper(); 
        cls_data objData = new cls_data();
        
        Integer otpSize = otp.length();
        if(otpSize == 5)
        {
            
            list<contact> conList = [Select id,Do_Not_Mail__c,Email_Verification_Status__c, Email_Verification_Status_Date__c,LastName from contact where email =: email Limit 1];
            
            if(conList != null && conList.size() > 0 )  // If the email id is exist then ...
            {
                objData.contact_exists = true;
                
                for(contact c : conList)
                {
                    objData.contact_id =c.id;
                    objData.name=c.LastName;
                    if(c.Do_Not_Mail__c == false && c.Email_Verification_Status__c == null && c.Email_Verification_Status_Date__c == null)
                    {
                        
                        OtpViaEmail.sendEmail(email,otp);  //Sending Email 
                        objData.otp_sms_sent = true; 
                        objData.otp_email_sent = true;
                        
                    }else 
                    {
                        objData.bounced_email=true;
                    }
                    
                }
                returnResponse.obj = objData;
                System.debug('returnResponse:'+returnResponse);
                return returnResponse;
                
            }
            else    // If the email id not exist then...
            {
                if(send_otp_if_exists==false)
                {
                    objData.contact_exists = false;
                    objData.contact_id =null;
                    OtpViaEmail.sendEmail(email,otp);    //Sending Email 
                    objData.otp_sms_sent = true; 
                    objData.otp_email_sent = true;
                    returnResponse.obj =objData;
                    System.debug('returnResponse:'+returnResponse);
                    return returnResponse;
                }
                
                objData.contact_exists = null;
                objData.contact_id =null;
                objData.otp_sms_sent = false; 
                objData.otp_email_sent = false;
                objData.name=null;
                returnResponse.obj = objData;
                System.debug('returnResponse:'+returnResponse);
                return returnResponse;         
                
            }
            
        }
        objData.contact_exists = null;
        objData.contact_id =null;
        objData.otp_sms_sent = false; 
        objData.otp_email_sent = false;
        objData.name=null;
        returnResponse.obj = objData;
        System.debug('returnResponse:'+returnResponse);
        return returnResponse; 
        
        
        
    }

    global class FinalReturnWrapper {
        
        public cls_data obj ;
        
        
    }
    public class cls_data {
        
        public boolean contact_exists;
        public boolean otp_sms_sent;
        public boolean otp_email_sent;
        public string contact_id;
        public boolean bounced_email;     
        public string name;
    }
    
    
    
}

_________________________________
Now How can I create the Test class which covered the if else and if not that then how can I cover the code covargae to more than 75 please advise.

Below test class is coverd 1 scenario which will be 35 percent code covarge 

Test Class :- 

@IsTest
public class WordpressIntegrationForEmailTest {
    
     @testsetup
    static void datasetup(){
        
         contact c = new contact ();
        c.LastName = 'Pav';
        c.Email = 'pavan@intuitiolabs.com';
        c.Do_Not_Mail__c=false;
        c.Email_Verification_Status__c=null;
        c.Email_Verification_Status_Date__c=null;
        insert c;
        
    }
    
    public static testMethod void  testPostRestService(){
        
        
        Test.startTest();        
        
        string email = 'pavan1@intuitiolabs.com';
        string otp='11223';
        string mob='987654321';
        boolean send_otp_if_exists = false;
        
        
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        
        req.requestURI = '/services/apexrest/WordpressIntegrationForEmail';  //Request URL
        req.httpMethod = 'POST';//HTTP Request Type
        req.requestBody = Blob.valueof('TestMsg');
        RestContext.request = req;
        RestContext.response= res;
    
       WordpressIntegrationForEmail.SendEmailMsg(email,otp,mob,send_otp_if_exists);
    
        Test.stopTest();
    }
    
   
    
}

Abdul KhatriAbdul Khatri
Hi Hitesh,

Please use the below test class will give 80% coverage. I added three test methods, all can be covered in one but as best practices, I split them. Please note you also have not placed system.assert which is the main reason for the test class.
 
@IsTest
public class WordpressIntegrationForEmailTest {
    
    @testsetup
    static void datasetup(){
        
        contact c = new contact ();
        c.LastName = 'Pav';
        c.Email = 'pavan@intuitiolabs.com';
        c.Do_Not_Mail__c=false;
        c.Email_Verification_Status__c=null;
        c.Email_Verification_Status_Date__c=null;
        insert c;
        
    }
    
    public static testMethod void testPostRestService_withNoContactAndOptSize5(){
        
        
        Test.startTest();        
        
        string email = 'pavan1@intuitiolabs.com';
        string otp='11223';
        string mob='987654321';
        boolean send_otp_if_exists = false;
        
        
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        
        req.requestURI = '/services/apexrest/WordpressIntegrationForEmail';  //Request URL
        req.httpMethod = 'POST';//HTTP Request Type
        req.requestBody = Blob.valueof('TestMsg');
        RestContext.request = req;
        RestContext.response= res;
        
        WordpressIntegrationForEmail.SendEmailMsg(email,otp,mob,send_otp_if_exists);
        
        Test.stopTest();
    }
    
    public static testMethod void testPostRestService_withContactAndOptSize5(){
        
        
        Test.startTest();        
        
        string email = 'pavan@intuitiolabs.com';
        string otp='11223';
        string mob='987654321';
        boolean send_otp_if_exists = false;
        
        
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        
        req.requestURI = '/services/apexrest/WordpressIntegrationForEmail';  //Request URL
        req.httpMethod = 'POST';//HTTP Request Type
        req.requestBody = Blob.valueof('TestMsg');
        RestContext.request = req;
        RestContext.response= res;
        
        WordpressIntegrationForEmail.SendEmailMsg(email,otp,mob,send_otp_if_exists);
        
        Test.stopTest();
    } 
    
    public static testMethod void testPostRestService_withOptNotSize5(){
        
        
        Test.startTest();        
        
        string email = 'pavan@intuitiolabs.com';
        string otp='112';
        string mob='987654321';
        boolean send_otp_if_exists = false;
        
        
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        
        req.requestURI = '/services/apexrest/WordpressIntegrationForEmail';  //Request URL
        req.httpMethod = 'POST';//HTTP Request Type
        req.requestBody = Blob.valueof('TestMsg');
        RestContext.request = req;
        RestContext.response= res;
        
        WordpressIntegrationForEmail.SendEmailMsg(email,otp,mob,send_otp_if_exists);
        
        Test.stopTest();
    }    
    
}

The purpose of the test class is not only coverage and deployment, it's the main purpose is to test your functionality and if it is giving you the expected results as per your requirement.

I hope this clarifies my insistencet on putting system.assert.

Regards