+ Start a Discussion
Neeraj Sharma 103Neeraj Sharma 103 

How to write test Class for below apex class controller

Please Help me to write test class for below apex class controller


abc.apexclass


public class EmailInvoiceA {
    
    @auraEnabled
    public static void sendMailWithPDF(String recordId){
        
        for(Invoice__c cc : [select Id,Distributor__r.E_mail__c,Distributor__r.CC_to_if_any__c,Distributor__r.Name from Invoice__c WHERE Id =: recordId]){
            list<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();  
            Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
            List<String> sendTo = new List<String>();//to string
        
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
            PageReference pref = page.PdfEmailInvoice;
            pref.getParameters().put('Id',recordId);
            pref.setRedirect(true);
            Blob b;
            b = pref.getContent();
            attach.setFileName('Invoice.pdf');
            attach.setBody(b);
            semail.setSubject('Invoice details');
            semail.setReplyTo('noreply@gmail.com');
            semail.setSenderDisplayName('salesforce User');
           
           sendTo.add(cc.Distributor__r.E_mail__c);
            
            List<String> send = new List<String>();//cc string
            system.debug('sendlis'+send);
        //    if(cc.Distributor__r.CC_to_if_any__c != null)
      //      {
        //         send.add(cc.Distributor__r.CC_to_if_any__c);
                 
     //       }
            
            if(cc.Distributor__r.E_mail__c==null && cc.Distributor__r.CC_to_if_any__c != null)
            {  
                
                send.add(cc.Distributor__r.CC_to_if_any__c);
               //semail.setccAddresses(sendTo);
               semail.setToAddresses(send);
           
            }
            else if(cc.Distributor__r.E_mail__c !=null && cc.Distributor__r.CC_to_if_any__c != null)
            {
                sendTo.add(cc.Distributor__r.E_mail__c);
                send.add(cc.Distributor__r.CC_to_if_any__c);
                
              semail.setccAddresses(send);
              semail.setToAddresses(sendTo);
                
           }
           else if(cc.Distributor__r.E_mail__c !=null && cc.Distributor__r.CC_to_if_any__c == null)
            {
                
              sendTo.add(cc.Distributor__r.E_mail__c);
               
                   semail.setToAddresses(sendTo);
         }
       
        
                mails.add(semail);
            String emailBody =' <html><body><p> Dear '+ cc.Distributor__r.Name+' ,<br><br> Please find the attached Invoice details. <br><br>    Regards, <br> IT Team <br>  Sayaji Groups.</p></body></html>';
            semail.setHtmlBody(emailBody);
            semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
        }
    }    
  
    
    @auraEnabled
     public static void sendMailWith(String recordId,String mMail){
        
        for(Invoice__c cc : [select Id,Distributor__r.E_mail__c,Distributor__r.CC_to_if_any__c,Distributor__r.Name from Invoice__c WHERE Id =: recordId]){
            list<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();  
            Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
            List<String> sendTo = new List<String>();
            sendTo.add(mMail);
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
            PageReference pref = page.PdfEmailInvoice;
            pref.getParameters().put('Id',recordId);
            pref.setRedirect(true);
            Blob b;
            b = pref.getContent();
            attach.setFileName('Invoice.pdf');
            attach.setBody(b);
            semail.setSubject('Invoice details');
            semail.setReplyTo('noreply@gmail.com');
            semail.setSenderDisplayName('salesforce User');
           
            //sendTo.add(cc.Distributor__r.E_mail__c);
              List<String> send = new List<String>();
            if(cc.Distributor__r.CC_to_if_any__c != null)
            {
                 send.add(cc.Distributor__r.CC_to_if_any__c);
                 
            }
         
            mails.add(semail);
            semail.setCcAddresses(send);
            semail.setToAddresses(sendTo);
            String emailBody =' <html><body><p> Dear '+ cc.Distributor__r.Name+' ,<br><br> Please find the attached Invoice details. <br><br>    Regards, <br> IT Team <br>  Sayaji Groups.</p></body></html>';
            semail.setHtmlBody(emailBody);
            semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
            system.debug('email send');
        }
    }    
  
    
     @auraEnabled
   public static void Mail(String recordId,String mMail,boolean checkCmp){
        
        for(Invoice__c cc : [select Id,Distributor__r.E_mail__c,Distributor__r.CC_to_if_any__c,Distributor__r.Name from Invoice__c WHERE Id =: recordId]){
            list<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();  
            Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
            List<String> sendTo = new List<String>();//to string
           
              system.debug(checkCmp);  
           
       //     semail.setToAddresses(sendTo);
          //shrutinigam1998@gmail.com  
        
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
            PageReference pref = page.PdfEmailInvoice;
            pref.getParameters().put('Id',recordId);
            pref.setRedirect(true);
            Blob b;
            b = pref.getContent();
            attach.setFileName('Invoice.pdf');
            attach.setBody(b);
            semail.setSubject('Invoice details');
            semail.setReplyTo('noreply@gmail.com');
            semail.setSenderDisplayName('salesforce User');
            
            
            
           if(checkCmp==true)
               {
                  sendTo.add(mMail);
                   semail.setToAddresses(sendTo);
               }
           else  if(checkCmp==false)
            {
               system.debug('hi check'+checkCmp);
              if(mMail != null)
            {
                 system.debug('hi mMail'+mMail);
                sendTo.add(mMail);
                semail.setToAddresses(sendTo);
            }
                
             if(cc.Distributor__r.E_mail__c != null)
            { system.debug('hi Distributor mail'+cc.Distributor__r.E_mail__c);
               sendTo.add(cc.Distributor__r.E_mail__c);  
                semail.setToAddresses(sendTo);
            } 
                
             if(cc.Distributor__r.CC_to_if_any__c != null)
   
            { 
                system.debug('hi Distributor cc mail'+cc.Distributor__r.CC_to_if_any__c);
                List<String> send = new List<String>();//cc string
                send.add(cc.Distributor__r.CC_to_if_any__c);  
               semail.setCcAddresses(send);
           }   
                
                
                
            }
            
      
            mails.add(semail);

            String emailBody =' <html><body><p> Dear '+ cc.Distributor__r.Name+' ,<br><br> Please find the attached Invoice details. <br><br>    Regards, <br> IT Team <br>  Sayaji Groups.</p></body></html>';
            semail.setHtmlBody(emailBody);
            semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
            system.debug('email send');
        }
    }       
}
Best Answer chosen by Neeraj Sharma 103
Ajay K DubediAjay K Dubedi
Hi Neeraj,
Firstly in your code, you have to change this for the test class as:
Controller:
public class EmailInvoiceA {
    
    @auraEnabled
    public static void sendMailWithPDF(String recordId){
        
        for(Invoice__c cc : [select Id,Distributor__r.E_mail__c,Distributor__r.CC_to_if_any__c,Distributor__r.Name from Invoice__c WHERE Id =: recordId]){
            list<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();  
            Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
            List<String> sendTo = new List<String>();//to string
            
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
            PageReference pref = page.PdfEmailInvoice;
            pref.getParameters().put('Id',recordId);
            pref.setRedirect(true);
            Blob b;
            if(Test.isRunningTest()) { 
                b = blob.valueOf('Unit.Test');
            } else {
                b = pref.getContent();
            }
            //b = pref.getContent();
            attach.setFileName('Invoice.pdf');
            attach.setBody(b);
            semail.setSubject('Invoice details');
            semail.setReplyTo('noreply@gmail.com');
            semail.setSenderDisplayName('salesforce User');
            
            sendTo.add(cc.Distributor__r.E_mail__c);
            
            List<String> send = new List<String>();//cc string
            system.debug('sendlis'+send);
            //    if(cc.Distributor__r.CC_to_if_any__c != null)
            //      {
            //         send.add(cc.Distributor__r.CC_to_if_any__c);
            
            //       }
            
            if(cc.Distributor__r.E_mail__c==null && cc.Distributor__r.CC_to_if_any__c != null)
            {  
                
                send.add(cc.Distributor__r.CC_to_if_any__c);
                //semail.setccAddresses(sendTo);
                semail.setToAddresses(send);
                
            }
            else if(cc.Distributor__r.E_mail__c !=null && cc.Distributor__r.CC_to_if_any__c != null)
            {
                sendTo.add(cc.Distributor__r.E_mail__c);
                send.add(cc.Distributor__r.CC_to_if_any__c);
                
                semail.setccAddresses(send);
                semail.setToAddresses(sendTo);
                
            }
            else if(cc.Distributor__r.E_mail__c !=null && cc.Distributor__r.CC_to_if_any__c == null)
            {
                
                sendTo.add(cc.Distributor__r.E_mail__c);
                
                semail.setToAddresses(sendTo);
            }
            
            
            mails.add(semail);
            String emailBody =' <html><body><p> Dear '+ cc.Distributor__r.Name+' ,<br><br> Please find the attached Invoice details. <br><br>    Regards, <br> IT Team <br>  Sayaji Groups.</p></body></html>;
 semail.setHtmlBody(emailBody);
            semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
            system.debug('email send');
        }
    }       
}
So, this
"b = pref.getContent();" will become 
"if(Test.isRunningTest()) { 
                b = blob.valueOf('Unit.Test');
            } else {
                b = pref.getContent();
            }"
For test class try this code and change code accordingly:
@isTest
private class EmailInvoiceA_Test {
   
     @isTest static void testsendMailWithPDF2()
    {
        Distributor__c dObj = new Distributor__c();
        dObj.Name = 'Test';
        dObj.CC_to_if_any__c = 'name@gmail.com';
        //dObj.E_mail__c = 'name@gmail.com';
        //Put all required field.
        insert dObj;
        
        Invoice__c Iobj = new Invoice__c();
        Iobj.Name = 'Test';
        Iobj.Distributor__c = dObj.Id;
        insert Iobj;
        
        EmailInvoiceA.sendMailWithPDF(Iobj.Id);
    }
    @isTest static void testsendMailWithPDF3()
    {
        Distributor__c dObj = new Distributor__c();
        dObj.Name = 'Test';
        //dObj.CC_to_if_any__c = 'name@gmail.com';
        dObj.E_mail__c = 'name@gmail.com';
        //Put all required field.
        insert dObj;
        
        Invoice__c Iobj = new Invoice__c();
        Iobj.Name = 'Test';
        Iobj.Distributor__c = dObj.Id;
        insert Iobj;
        
        EmailInvoiceA.sendMailWithPDF(Iobj.Id);
    }
    
     @isTest static void testsendMailWith2()
    {
        string Email = 'name@gmail.com';
        Distributor__c dObj = new Distributor__c();
        dObj.Name = 'Test';
        dObj.CC_to_if_any__c = 'name@gmail.com';
        //dObj.E_mail__c = 'name@gmail.com';
        //Put all required field.
        insert dObj;
        
        Invoice__c Iobj = new Invoice__c();
        Iobj.Name = 'Test';
        Iobj.Distributor__c = dObj.Id;
        insert Iobj;
        
        EmailInvoiceA.sendMailWith(Iobj.Id, Email);
    }
    @isTest static void testsendMailWith3()
    {
        string Email = 'name@gmail.com';
        Distributor__c dObj = new Distributor__c();
        dObj.Name = 'Test';
        //dObj.CC_to_if_any__c = 'name@gmail.com';
        dObj.E_mail__c = 'name@gmail.com';
        //Put all required field.
        insert dObj;
        
        Invoice__c Iobj = new Invoice__c();
        Iobj.Name = 'Test';
        Iobj.Distributor__c = dObj.Id;
        insert Iobj;
        
        EmailInvoiceA.sendMailWith(Iobj.Id, Email);
    }
    
     @isTest static void testMail2()
    {
        boolean checkCmp = False;
        string Email = 'name@gmail.com';
        Distributor__c dObj = new Distributor__c();
        dObj.Name = 'Test';
        dObj.CC_to_if_any__c = 'name@gmail.com';
        //dObj.E_mail__c = 'name@gmail.com';
        //Put all required field.
        insert dObj;
        
        Invoice__c Iobj = new Invoice__c();
        Iobj.Name = 'Test';
        Iobj.Distributor__c = dObj.Id;
        insert Iobj;
        
        EmailInvoiceA.Mail(Iobj.Id, Email, checkCmp);
    }
    @isTest static void testMail3()
    {
        boolean checkCmp = False;
        string Email = 'name@gmail.com';
        Distributor__c dObj = new Distributor__c();
        dObj.Name = 'Test';
        //dObj.CC_to_if_any__c = 'name@gmail.com';
        dObj.E_mail__c = 'name@gmail.com';
        //Put all required field.
        insert dObj;
        
        Invoice__c Iobj = new Invoice__c();
        Iobj.Name = 'Test';
        Iobj.Distributor__c = dObj.Id;
        insert Iobj;
        
        EmailInvoiceA.Mail(Iobj.Id, Email, checkCmp);
    }
    @isTest static void testMail4()
    {
        boolean checkCmp = True;
        string Email = 'name@gmail.com';
        Distributor__c dObj = new Distributor__c();
        dObj.Name = 'Test';
        dObj.CC_to_if_any__c = 'Test';
        dObj.E_mail__c = 'name@gmail.com';
        //Put all required field.
        insert dObj;
        
        Invoice__c Iobj = new Invoice__c();
        Iobj.Name = 'Test';
        Iobj.Distributor__c = dObj.Id;
        insert Iobj;
        
        EmailInvoiceA.Mail(Iobj.Id, Email, checkCmp);
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi

All Answers

PINKY REGHUPINKY REGHU
Hi,
public class EmailInvoiceAtest {
    @testSetup static void setup() {
            Distributor__c distrib= new Distributor__c();
          	// enter the field values
          insert distrib;
    		Invoice__c invoice= new Invoice__c();
     		// enter the field values
     		// invoice.Distributor__c=distrib.id;
     		insert invoice;
    	}
      static testMethod void sendMailWithPDFTest() {
          EmailInvoiceA.sendMailWithPDF(invoice.id);
         }
      static testMethod void sendMailWithTest(){  {
          EmailInvoiceA.sendMailWith(invoice.id,'xxxxx@gmail.com');
         }     
     static testMethod void MailTest() {
          EmailInvoiceA.Mail(invoice.id,'xxxxx@gmail.com','true');
         }
}

Hope this helps.
Neeraj Sharma 103Neeraj Sharma 103
Hi PINKY REGHU

your code only 6%Code Coverage 
Ajay K DubediAjay K Dubedi
Hi Neeraj,
Firstly in your code, you have to change this for the test class as:
Controller:
public class EmailInvoiceA {
    
    @auraEnabled
    public static void sendMailWithPDF(String recordId){
        
        for(Invoice__c cc : [select Id,Distributor__r.E_mail__c,Distributor__r.CC_to_if_any__c,Distributor__r.Name from Invoice__c WHERE Id =: recordId]){
            list<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();  
            Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
            List<String> sendTo = new List<String>();//to string
            
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
            PageReference pref = page.PdfEmailInvoice;
            pref.getParameters().put('Id',recordId);
            pref.setRedirect(true);
            Blob b;
            if(Test.isRunningTest()) { 
                b = blob.valueOf('Unit.Test');
            } else {
                b = pref.getContent();
            }
            //b = pref.getContent();
            attach.setFileName('Invoice.pdf');
            attach.setBody(b);
            semail.setSubject('Invoice details');
            semail.setReplyTo('noreply@gmail.com');
            semail.setSenderDisplayName('salesforce User');
            
            sendTo.add(cc.Distributor__r.E_mail__c);
            
            List<String> send = new List<String>();//cc string
            system.debug('sendlis'+send);
            //    if(cc.Distributor__r.CC_to_if_any__c != null)
            //      {
            //         send.add(cc.Distributor__r.CC_to_if_any__c);
            
            //       }
            
            if(cc.Distributor__r.E_mail__c==null && cc.Distributor__r.CC_to_if_any__c != null)
            {  
                
                send.add(cc.Distributor__r.CC_to_if_any__c);
                //semail.setccAddresses(sendTo);
                semail.setToAddresses(send);
                
            }
            else if(cc.Distributor__r.E_mail__c !=null && cc.Distributor__r.CC_to_if_any__c != null)
            {
                sendTo.add(cc.Distributor__r.E_mail__c);
                send.add(cc.Distributor__r.CC_to_if_any__c);
                
                semail.setccAddresses(send);
                semail.setToAddresses(sendTo);
                
            }
            else if(cc.Distributor__r.E_mail__c !=null && cc.Distributor__r.CC_to_if_any__c == null)
            {
                
                sendTo.add(cc.Distributor__r.E_mail__c);
                
                semail.setToAddresses(sendTo);
            }
            
            
            mails.add(semail);
            String emailBody =' <html><body><p> Dear '+ cc.Distributor__r.Name+' ,<br><br> Please find the attached Invoice details. <br><br>    Regards, <br> IT Team <br>  Sayaji Groups.</p></body></html>;
 semail.setHtmlBody(emailBody);
            semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
            system.debug('email send');
        }
    }       
}
So, this
"b = pref.getContent();" will become 
"if(Test.isRunningTest()) { 
                b = blob.valueOf('Unit.Test');
            } else {
                b = pref.getContent();
            }"
For test class try this code and change code accordingly:
@isTest
private class EmailInvoiceA_Test {
   
     @isTest static void testsendMailWithPDF2()
    {
        Distributor__c dObj = new Distributor__c();
        dObj.Name = 'Test';
        dObj.CC_to_if_any__c = 'name@gmail.com';
        //dObj.E_mail__c = 'name@gmail.com';
        //Put all required field.
        insert dObj;
        
        Invoice__c Iobj = new Invoice__c();
        Iobj.Name = 'Test';
        Iobj.Distributor__c = dObj.Id;
        insert Iobj;
        
        EmailInvoiceA.sendMailWithPDF(Iobj.Id);
    }
    @isTest static void testsendMailWithPDF3()
    {
        Distributor__c dObj = new Distributor__c();
        dObj.Name = 'Test';
        //dObj.CC_to_if_any__c = 'name@gmail.com';
        dObj.E_mail__c = 'name@gmail.com';
        //Put all required field.
        insert dObj;
        
        Invoice__c Iobj = new Invoice__c();
        Iobj.Name = 'Test';
        Iobj.Distributor__c = dObj.Id;
        insert Iobj;
        
        EmailInvoiceA.sendMailWithPDF(Iobj.Id);
    }
    
     @isTest static void testsendMailWith2()
    {
        string Email = 'name@gmail.com';
        Distributor__c dObj = new Distributor__c();
        dObj.Name = 'Test';
        dObj.CC_to_if_any__c = 'name@gmail.com';
        //dObj.E_mail__c = 'name@gmail.com';
        //Put all required field.
        insert dObj;
        
        Invoice__c Iobj = new Invoice__c();
        Iobj.Name = 'Test';
        Iobj.Distributor__c = dObj.Id;
        insert Iobj;
        
        EmailInvoiceA.sendMailWith(Iobj.Id, Email);
    }
    @isTest static void testsendMailWith3()
    {
        string Email = 'name@gmail.com';
        Distributor__c dObj = new Distributor__c();
        dObj.Name = 'Test';
        //dObj.CC_to_if_any__c = 'name@gmail.com';
        dObj.E_mail__c = 'name@gmail.com';
        //Put all required field.
        insert dObj;
        
        Invoice__c Iobj = new Invoice__c();
        Iobj.Name = 'Test';
        Iobj.Distributor__c = dObj.Id;
        insert Iobj;
        
        EmailInvoiceA.sendMailWith(Iobj.Id, Email);
    }
    
     @isTest static void testMail2()
    {
        boolean checkCmp = False;
        string Email = 'name@gmail.com';
        Distributor__c dObj = new Distributor__c();
        dObj.Name = 'Test';
        dObj.CC_to_if_any__c = 'name@gmail.com';
        //dObj.E_mail__c = 'name@gmail.com';
        //Put all required field.
        insert dObj;
        
        Invoice__c Iobj = new Invoice__c();
        Iobj.Name = 'Test';
        Iobj.Distributor__c = dObj.Id;
        insert Iobj;
        
        EmailInvoiceA.Mail(Iobj.Id, Email, checkCmp);
    }
    @isTest static void testMail3()
    {
        boolean checkCmp = False;
        string Email = 'name@gmail.com';
        Distributor__c dObj = new Distributor__c();
        dObj.Name = 'Test';
        //dObj.CC_to_if_any__c = 'name@gmail.com';
        dObj.E_mail__c = 'name@gmail.com';
        //Put all required field.
        insert dObj;
        
        Invoice__c Iobj = new Invoice__c();
        Iobj.Name = 'Test';
        Iobj.Distributor__c = dObj.Id;
        insert Iobj;
        
        EmailInvoiceA.Mail(Iobj.Id, Email, checkCmp);
    }
    @isTest static void testMail4()
    {
        boolean checkCmp = True;
        string Email = 'name@gmail.com';
        Distributor__c dObj = new Distributor__c();
        dObj.Name = 'Test';
        dObj.CC_to_if_any__c = 'Test';
        dObj.E_mail__c = 'name@gmail.com';
        //Put all required field.
        insert dObj;
        
        Invoice__c Iobj = new Invoice__c();
        Iobj.Name = 'Test';
        Iobj.Distributor__c = dObj.Id;
        insert Iobj;
        
        EmailInvoiceA.Mail(Iobj.Id, Email, checkCmp);
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
This was selected as the best answer
Neeraj Sharma 103Neeraj Sharma 103
Hi Ajay K Dubedi
Thanks For your help Now test class code coverage is 93%