+ Start a Discussion
Apex developer 21Apex developer 21 

Help writing a test class PDF attachment

Could someone help me write a test class for this:
 
private final Facturatie__c a;

    public attachPDF(ApexPages.StandardController standardPageController) {
        a = (Facturatie__c)standardPageController.getRecord(); 
      
    }    
    Facturatie__c  currentRecord = [SELECT id, Accountname__r.Name FROM Facturatie__c WHERE id = :ApexPages.currentPage().getParameters().get('id')];

    public PageReference attachPDF() {

        PageReference pdfPage = Page.Factuur2PDF;
        pdfPage.getParameters().put('id',a.id);
          
        Blob pdfBlob = pdfPage.getContent(); 
        
        Attachment attach = new Attachment(parentId = a.id, Name = 'Factuur ' + '-' + currentRecord.Accountname__r.Name +'-'+ date.today().format() +'.pdf', body = pdfBlob); 
        insert attach;
        
        PageReference pageWhereWeWantToGo = new ApexPages.StandardController(a).view(); 
        pageWhereWeWantToGo.setRedirect(true); 
        return pageWhereWeWantToGo;
    }
}

 
Best Answer chosen by Apex developer 21
Santosh Reddy9989Santosh Reddy9989
Hi,

@isTest
private class ExtensionTests
{
    
    @isTest static void test_method_one()
    {
      Test.startTest();
      Accountname__c a = new Accountname__c();
      Facturatie__c testobj = new Facturatie__c();

       a.name = 'test';
       // initialize all fields of Accountname__c object here
       insert a;

       testobj.Name = 'test';
       // initialize all fields of Facturatie__c object here
       insert testobj;
        
        ApexPages.CurrentPage().getParameters().put('id',testobj.id);
       Apexpages.StandardController sc = new Apexpages.StandardController(testobj);
       ExtensionClassname ext = new ExtensionClassname(sc);
       PageReference ref = ext.attachPDF();

    }
   }