+ Start a Discussion
uuuu 

Help me to write test class for SendEmailPDFClass

Hey Friends,
Please help me to write test class for following apex class.
global class sendPDFEmailClass{
    public sendPDFEmailClass(){}
        public sendPDFEmailClass(ApexPages.StandardController controller) {
    }
    
    webservice static void SendEmail(){
        Id recordId=ApexPages.currentPage().getParameters().get('Id');
        Monthly_Salary__c inv = [Select Id, Name ,Resource__c From Monthly_Salary__c Where Id=:recordId];
        Id ResourceID=inv.Resource__c;
        Resource__c res=[select id, Name, Official_Email__c,Personal_Email__c from Resource__c where id=:ResourceID];
        PageReference pdf = Page.SalarySlipPage;
        pdf.getParameters().put('id', recordId);
        pdf.setRedirect(true);
        system.debug(pdf.getContentAsPDF());
        Blob b;
        
        if (Test.IsRunningTest()){
            b=Blob.valueOf('this is test');
        }else{
            b = pdf.getContentAsPDF();
        }
        // Create Attachment Object to attach with Email
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName(inv.Name+'.pdf');
        efa.setBody(b);
        // Define the email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        // Sets the paramaters of the email
        email.setSubject('Salary Slip - '+inv.Name);
        if(res.Personal_Email__c!=Null){
            email.setCCAddresses( new List<String>{res.Personal_Email__c} );}
        if(res.Official_Email__c==Null){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING,'The Resource has no Official email Address.'));
        }
        else{
            email.setToAddresses( new List<String>{res.Official_Email__c} );
        }
        email.setHtmlBody('Hi '+res.Name+',<br/><br/> '+'Please find attached your salary slip for the month of '+inv.Name+
                          +'<br/><br/>'+'Thank You,'+'<br/>'
                          +'Anita');
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
        try{
            Messaging.SendEmailResult [] result = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        }catch(System.Exception e){
            
        }
    }
}

Below is the test class
@IsTest
public class sendPDFEmailClassTest {
    
        @IsTest public static void SendemailControllerTestClass() {
        Resource__c res=new Resource__c();
        res.Name='TestR';
        res.Company__c='Nanostuffs';
        res.Official_Email__c='akshata.shah@nanostuffs.com';
        res.Personal_Email__c='test@gmail.com';
        insert res;
        
        Monthly_Salary__c m=new Monthly_Salary__c();
        m.Resource__c=res.Id;
        m.From_Date__c=date.newInstance(2020, 11,1);
        m.To_Date__c=date.newInstance(2020, 11, 30);
        
        insert m;
        
    }
}

I m not getting how to write test class for SingleEmailMessage
Please help me
Thank in advance
Regards,
Anita
AbhishekAbhishek (Salesforce Developers) 
Hi Anita,

This is the code snippet

@isTest
private class Test_PdfEmailController {

  

  static testMethod void testPdfEmailer() {

  
    Account  account = new Account();
    account.Name = 'Test Account';
    insert account;
    
    
    Opportunity opp = new Opportunity();
    opp.Name ='Demo' ;
    opp.Amount='123';
    opp.Stage='Closed Won' ; 
    opp.CloseDate = System.today();
    insert opp ;

    
    PageReference pref = Page.Demo;
    pref.getParameters().put('id',opp.id);
    Test.setCurrentPage(pref);

    GeneratePurchaseContract  con = new GeneratePurchaseContract ();    

    Test.startTest();

 con.save();
 
 
    Test.stopTest(); 

  }

You have to make minor changes as per your requirement.


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks.