This question already has an answer here:

I have a requirement to send a specific report as an attachment on schedule basis.

So, i have created a schedule class with future annotation to send.But now when i'm trying to setup apex class I'm getting below error.

Error Message Methods defined as TestMethod do not support getContent call

Apex class:

global class CallSubscriptionReportScheduler {
    public static void SentSubscriptionData()
        Id userdetail=[Select id from user where email='xyz@gmail.com'].id;
        OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'info@xyz.com' Limit 1];
        ApexPages.PageReference report = new ApexPages.PageReference('/00O6F00000CGpHH?csv=1');
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
        message.setPlainTextBody('The report is attached.');
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } ); 

Test class:

public class TestWeeklySubscriptionData {
    public static String CRON_EXP = '0 0 1 ? * MON';
    static testmethod void testsubscription() {        
            WeeklySubscriptionData uca = new WeeklySubscriptionData();
            String jobId = System.schedule('ScheduledApexTest',CRON_EXP,new WeeklySubscriptionData());
            // after the testing stops, assert records were updated properly
            //System.assertEquals(6, [select count() from Lead where Lead_Type__c='Insurance' AND RecordTypeId ='0126F0000016MZP']);

Could someone help me.

marked as duplicate by Tushar Sharma, Pranay Jaiswal, Derek F, battery.cord, glls May 20 at 20:01

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.

  • Best you include the error message. – Keith C May 20 at 7:44
  • I have updated my question @KeithC – Pavan tej May 20 at 7:53

I have found no way to handle this other than using the last resort Test.isRunningTest:

PageReference pr = ...;

Blob pdf = !Test.isRunningTest() ? pr.getContentAsPdf() : Blob.valueOf('Fake content');

or in your class:

attachment.setBody(!Test.isRunningTest() ? report.getContent() : Blob.valueOf('Fake content'));
  • Hey sorry could you please me know how to setup this in my test class. i don't have any idea about this – Pavan tej May 20 at 8:04
  • 1
    Added @Pavantej. – Keith C May 20 at 8:08
  • Works likes a charm. But i have a doubt can we use mockclass instead of this? – Pavan tej May 20 at 13:48
  • 1
    @Pavantej Mocking for this isn't possible AFAIK. Web service callouts should use mocking not this technique. – Keith C May 20 at 13:53

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