-1

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 {
@future(callout=true)
    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();
        attachment.setFileName('Data_Report.csv');
        attachment.setBody(report.getContent());
        attachment.setContentType('text/csv');
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
        message.setSubject('Data');
        message.setPlainTextBody('The report is attached.');
        message.setOrgWideEmailAddressId(owea.get(0).Id);
        message.setReplyTo('sfdcadmin@xyz.com');
        message.setTargetObjectId(userdetail);
        message.setSaveAsActivity(false);
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } ); 
    }
}

Test class:

@isTest
public class TestWeeklySubscriptionData {
    public static String CRON_EXP = '0 0 1 ? * MON';
    static testmethod void testsubscription() {        
            Test.startTest();
            WeeklySubscriptionData uca = new WeeklySubscriptionData();
            String jobId = System.schedule('ScheduledApexTest',CRON_EXP,new WeeklySubscriptionData());
            Test.stopTest();
            // 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
1

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.