0

I have written a class which implements queueable interface and creates PDFs and saves them as attachments. Which is working fine.

public class ExpensePDF_Async_Helper implements Queueable, Database.AllowsCallouts{

            private Set<Id> idset = new Set<Id>();

            public ExpensePDF_Async_Helper(set<id> approvedIdset){

                idset = approvedIdset;            
            }

            public void execute(QueueableContext qc)
            {

                Integer allowedCallouts = Limits.getLimitCallouts() - Limits.getCallouts();

               if(allowedCallouts <=0)
               return;

               list<expense_report__c> pdfsTocreate = new list<expense_report__c>();

               for(Expense_report__c repo :[select PDF_created__c from expense_report__c where id in:Idset LIMIT :allowedCallouts])
               {
                   if(repo.PDF_created__c == false)
                   {
                   // callout for getContentasPDF
                   ExpenseReportPDF.savePDFattachment(repo.id);
                   repo.PDF_Created__c = true;  
                   pdfsTocreate.add(repo);           
                   }
               }
                update PDFsTocreate;// this line doesn't execute in Unit test
            }
    }

Test Class 1: I see that one statements of my original class doesn't get executes in debug log

//Initialization here

    Test.startTest();
    System.enqueueJob(new ExpensePDF_Async_Helper(setId));
    Test.stopTest();

     list<attachment> attach = [select id from attachment where parentid in :setId];

    System.assertEquals(2, attach.size());

    System.assertEquals(true, expReport2.PDF_created__c);

I see that below one statements of my original class doesn't get execute if I use Test Class 1 & Test Class 2

update PDFsTocreate;

Test Class 2: One statement of my original class doesn't get executes

    Test.startTest();
    ExpensePDF_Async_Helper q = new ExpensePDF_Async_Helper(setId);
    q.execute(null);
    Test.stopTest();

     list<attachment> attach = [select id from attachment where parentid in :setId];

    System.assertEquals(2, attach.size());

    System.assertEquals(true, expReport2.PDF_created__c);

The following statement doesn't get executed of my original class

update PDFsTocreate;

Can someone please explain why update statement in original class is not being called in both the test classes?

7
  • Just fyi, the results of Limits.getLimitCallouts() - Limits.getCallouts() are strictly non-negative. It doesn't make sense to check if they're less than 0.
    – Adrian Larson
    Commented Mar 6, 2016 at 14:55
  • Well, in your case you want ==, but yes, that's the idea.
    – Adrian Larson
    Commented Mar 6, 2016 at 17:23
  • Can you think of the reason why a DML statement in Queueable class won't execute when called from a test class as in my case?
    – Jarvis
    Commented Mar 6, 2016 at 17:47
  • FYI also, it seems you have dml in a for loop here
    – zokito
    Commented Mar 6, 2016 at 21:39
  • @zokito No, there's no DML inside loop, the indentation is clear.
    – Jarvis
    Commented Mar 7, 2016 at 7:15

1 Answer 1

1

The most likely reason for the error in coverage is because the line

ExpenseReportPDF.savePDFattachment(repo.id);

is probably executing getContentAsPDF() that, per the Apex Doc ...

If you use getContentAsPDF in a test method, the test method fails. getContentAsPDF is treated as a callout in API version 34.0 and later.

There is an Idea for fixing this

The inestimable @BobBuzzard has a simple workaround (i.e. if (Test.isRunningTest() ...) for this in his SFSE answer. Mohith has a similar workaround here

5
  • Yes, hidden exception seems a likely cause.
    – Adrian Larson
    Commented Mar 6, 2016 at 23:49
  • Eric, the class ExpenseReportPDF which makes callout. I have taken care of that Blob content = Test.isRunningTest() ? Blob.valueOf('UNIT.TEST') : pr.getContent();
    – Jarvis
    Commented Mar 7, 2016 at 7:22
  • @Bennie -- so the workaround solved your issue or the issue remains?
    – cropredy
    Commented Mar 7, 2016 at 17:19
  • Eric, the workaround you suggested was used for different class. This one is a different problem I believe.
    – Jarvis
    Commented Mar 7, 2016 at 19:10
  • @Bennie. So if you look at the debug logs you don't see any messages indicating that the test was stopped?
    – cropredy
    Commented Mar 7, 2016 at 19:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .