-1

This question already has an answer here:

I wrote a batch apex to send reports. PFB the main class and test class but not able get the code coverage. Please assist me.

public class BatchReportsAutoSend implements database.Batchable<sobject>,Database.AllowsCallouts {
    public transient list<Messaging.EmailFileAttachment> emailAttach;
    String reportName1;
    Set<String> processingList; 
    List<String> reportsList;

    /*
    public BatchReportsAutoSend(set<string> objs ){
       processingList = objs;   
       reportsList = new List<String>(objs); 
       reportName1 = reportsList[0];
    }
*/
    public Database.QueryLocator start(Database.BatchableContext bc) {
        list<string> reportListName = new list<string>();
        reportListName.add('Testing_Reports1');
        reportListName.add('Testing_Reports2');
       //String query = 'SELECT Id,name FROM Report where DeveloperName in :reportsList';
       //return Database.getQueryLocator(query);
        return Database.getQueryLocator('SELECT Id,name FROM Report where DeveloperName IN :reportListName'); 
    }
    public void execute(Database.BatchableContext bc, List<Report> scope){
        emailAttach = new list<Messaging.EmailFileAttachment>();
        for(Report rp : scope){         
            system.debug('OBJPAGE ##'+rp.Id);           
            ApexPages.PageReference objPage = new ApexPages.PageReference('/'+rp.Id+'?csv=1');
            system.debug('OBJPAGE ##'+objPage);
            Messaging.EmailFileAttachment objMsgEmailAttach = new Messaging.EmailFileAttachment();
            objMsgEmailAttach.setFileName(rp.name+'.csv');
            objMsgEmailAttach.setInline(false);
            Blob pageContent;
            if(!test.isRunningTest()){

            pageContent = objPage.getContent();
            objMsgEmailAttach.setBody(pageContent);
            objMsgEmailAttach.setContentType('text/csv');
            }else{
            pageContent = blob.valueof('TEST'); 
            }
            emailAttach.add(objMsgEmailAttach);
        }
            system.debug('Attachments'+emailAttach);
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {'smuti98@gmail.com'};
            mail.setUseSignature(false);
            mail.setToAddresses(toAddresses);
            mail.setSubject('Email from Salesforce');
            mail.setPlainTextBody('Hi Team, Please find the reports as you requested'); 
            mail.setFileAttachments(emailAttach);
            Messaging.SendEmailResult [] sendMsgResult = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail}); 

    }    
    public void finish(Database.BatchableContext bc){ 

    } 

}

Test Class:

@isTest(seealldata=true)
public class Test_BatchReportsAutoSend {
@isTest
static void runReport() { 
List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
PageReference pdf = Page.Report;
//pdf.getParameters().put('id', String.valueOf(testAccount.Id));
Test.setCurrentPage(pdf);

Blob b = blob.valueof('Unit.test');
try {b = pdf.getContentasPdf(); 
}catch(Exception e) {}

system.debug('The document page  is'+b);
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('FileName');
efa.setBody(b);

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'smuti98@gmail.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('Email from Salesforce');
mail.setPlainTextBody('Hi Team, Please find the reports as you requested'); 
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { efa });
emailList.add(mail);
List<Messaging.SendEmailResult> result = Messaging.sendEmail(emailList);
System.assert(result[0].isSuccess(), 'Email not sent');


Test.StartTest(); 
BatchReportsAutoSend bp = new  BatchReportsAutoSend();
Id jobId = Database.executeBatch(bp);
Test.stopTest();
}
}

marked as duplicate by Derek F, glls, battery.cord, Pranay Jaiswal, Reshma Feb 11 at 16:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Do you have 'Testing_Reports1' and 'Testing_Reports1' reports in your org? – Amit Feb 11 at 9:25
  • Yes @AmitGhadage while running batch i am able send the reports successfully. But the test class is causing the issue. – S R Mahapatra Feb 11 at 9:52
  • @DerekF This is not the duplicate of the recommended post. – S R Mahapatra Feb 11 at 12:52
  • 2
    In what way is it not a duplicate? From what I can see, you are asking about how to increase your coverage. – Derek F Feb 11 at 12:55
1

Your question is confusing because you don't actually have a code coverage issue. Your unit test is failing, which naturally does not provide code coverage - you only get coverage if your tests actually pass!

Based on the error message in the title of your question,

Send email failed, No body supplied for file attachment

the issue is here:

if(!test.isRunningTest()){
    pageContent = objPage.getContent();
    objMsgEmailAttach.setBody(pageContent);
    objMsgEmailAttach.setContentType('text/csv');
}else{
    pageContent = blob.valueof('TEST'); 
}

You forgot to finish setting up objMsgEmailAttach when Test.isRunningTest() is true. You need to do that like in the if branch.

You can also delete everything prior to Test.startTest() in your unit test because it does nothing at all. You do not need to test built-in platform code, and doing so makes your test confusing and may result in false results.

  • I did get you what your trying say in "You forgot to finish setting up objMsgEmailAttach when Test.isRunningTest() is true. You need to do that like in the if branch." this sentence. Can you please post the correct code for this. In test class we have to use the try/catch block for get a dummy attachment right. Actually while running testing the the attachment body is not coming it is coming as null. that is why the error is coming. Please assist me. Thanks for your response. – S R Mahapatra Feb 11 at 18:43
  • As I described in my answer, you are not populating the body of objMsgEmailAttach with dummy content when you are running the test. No, you do not need to use a try/catch block. – David Reed Feb 11 at 18:49
  • I got the solutions. Thanks for your response. – S R Mahapatra Feb 12 at 8:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.