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 

test class for integration class for below code

@RestResource(urlMapping='/AccountService/*')
global class RestAccountIntegration {
    
      
    @HTTPPOST
    
    global static string CreateAcc(string email , string otp)
    {
        string failed = 'Email not exist';   //Need to create custom label
        string success = 'Email successfully sent to email address' ;//Need to create custom label
        list<contact> conList = [Select id,Do_Not_Mail__c,Email_Verification_Status__c,    Email_Verification_Status_Date__c from contact where email =: email];
        
        if(conList.size() > 0 && conList.size() < 1  )  // If the email id is exist then ...
        {
            
            for(contact c : conList)
            {
                if(c.Do_Not_Mail__c == false && c.Email_Verification_Status__c == null && c.Email_Verification_Status_Date__c == null)
                {
                    //  EmailSending.emailSend(email,otp);
                    EmailSending.sendEmail(email,otp);
                    system.debug('Email Id :- '+email);
                    system.debug('OTP :- '+ otp);
                    return success ;
                }
                
            }
            
            return null ;
            
        }
        else    // If the email id not exist then...
        {
            return failed ;
            
        }
        
        
    }
    
}
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hitesh,
Can you try the below test class.
 
@istest
public class RestAccountIntegrationTest{
     static testmethod void triggerTest(){
        Contact con= new Contact();
         con.lastname='sample';
         con.email='sample@gmail.com';
         insert con;
         test.startTest();
         String message=RestAccountIntegration.CreateAcc('sample@gmail.com','1234');
         system.assertEquals('Email successfully sent to email address', message);
                  String message1=RestAccountIntegration.CreateAcc('sampssle@gmail.com','1234');
         system.assertEquals('Email not exist', message1);
        test.stopTest();
    }
}
Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
 
Hitesh AlgamwarHitesh Algamwar
Thank you Sai Praveen  for your help☺ Thats realy help ☺
Hitesh AlgamwarHitesh Algamwar

Hi Sai Praveen  

I have change the class to :- 

@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;
    }
    
}st
____________________
Can you advise how can I cover all the scenario in the test class here I am returning the wrapper class and previous we are returning string so we can test the 2 scenario but now scenario is different how we can write the test class. 

Test class is :- 

@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();
    }
    
   
    
}