function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Apex developer 21Apex developer 21 

testclass safe and attach pdf

I have the following controller extetion which works to make a pdf as attachment, but need some help writing a testclass for so far i have the following:
 
public with sharing class attachPDF {
private final Facturatie__c a;
    public attachPDF(ApexPages.StandardController standardPageController) {
        a = (Facturatie__c)standardPageController.getRecord(); //instantiate the Facturatie__c object for the current record  
    }    
    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); //create the attachment object
        insert attach;
        PageReference pageWhereWeWantToGo = new ApexPages.StandardController(a).view();
        pageWhereWeWantToGo.setRedirect(true);
        return pageWhereWeWantToGo;
    }
}
 
@isTest(seeAllData=true) 
public class attachPDFTestClass {

static testMethod void testAttachments() { 
    Facturatie__c a = new Facturatie__c(Facturatie__c.Accountname__r.Name='Test');
    insert a; 

    Attachment attach=new Attachment(); 
    attach.Name='Unit Test Attachment'; 
    Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body'); 
    attach.body=bodyBlob; attach.parentId=a.id;
    insert attach;

    List<Attachment> attachments=[select id, name from Attachment where parent.id=:a.id]; 
    System.assertEquals(1, attachments.size()); 

    Test.StartTest();
    FeedItem f = new FeedItem();
        f.ParentId = a.id;
        f.body = 'test';
        insert f;
        FeedComment fc = new FeedComment();
        fc.CommentBody = 'legal test';
        fc.FeedItemId = f.Id;
        insert fc;
        Test.StopTest();
        System.assertEquals ('legal test', fc.commentbody); 
    }
}

I get the error invalid field initializer
Best Answer chosen by Apex developer 21
Saurabh BSaurabh B
Apex,
you need to instantiate your controller extension.   For example, name of your extension class is "attachPDF", I dont see that you are calling it any where in the test class. You will never get a code coverage until you do that... try something like below and see last two lines,

 
@isTest
public class attachPDFTestClass {

static testMethod void testAttachments() { 
    
    Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		insert testAccount;
    
    
    Facturatie__c a = new Facturatie__c(Accountname__c = testAccount.Name);
    insert a; 

    Attachment attach=new Attachment(); 
    attach.Name='Unit Test Attachment'; 
    Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body'); 
    attach.body=bodyBlob; attach.parentId=a.id;
    insert attach;

    List<Attachment> attachments=[select id, name from Attachment where parent.id=:a.id]; 
    System.assertEquals(1, attachments.size()); 
    
    PageReference pageRef = Page.Factuur2PDF;
    Test.setCurrentPage(pageRef);

     ApexPages.StandardController sc = new ApexPages.StandardController(a);
     attachPDF aPDF = new attachPDF (sc);
   

    }
}


 

All Answers

JeffreyStevensJeffreyStevens
Inside of your Test.StartTest();  - add a call to the Page.Factuur2PDF;  - just like you did in your controller starting in line 8.
Apex developer 21Apex developer 21
Hi Jeffrey,

Could you show this by example?? I dont get the 'in line 8'
Saurabh BSaurabh B
Hi Apex,
Try adding below to your test class,
 
PageReference pageRef = Page.Factuur2PDF;
Test.setCurrentPage(pageRef);
Apex developer 21Apex developer 21
I tried the following without getting any code coverage: 
@isTest
public class attachPDFTestClass {

static testMethod void testAttachments() { 
    
    Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		insert testAccount;
    
    
    Facturatie__c a = new Facturatie__c(Accountname__c = testAccount.Name);
    insert a; 

    Attachment attach=new Attachment(); 
    attach.Name='Unit Test Attachment'; 
    Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body'); 
    attach.body=bodyBlob; attach.parentId=a.id;
    insert attach;

    List<Attachment> attachments=[select id, name from Attachment where parent.id=:a.id]; 
    System.assertEquals(1, attachments.size()); 
    
    PageReference pageRef = Page.Factuur2PDF;
    Test.setCurrentPage(pageRef);
    }
}


 
Saurabh BSaurabh B
Apex,
you need to instantiate your controller extension.   For example, name of your extension class is "attachPDF", I dont see that you are calling it any where in the test class. You will never get a code coverage until you do that... try something like below and see last two lines,

 
@isTest
public class attachPDFTestClass {

static testMethod void testAttachments() { 
    
    Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		insert testAccount;
    
    
    Facturatie__c a = new Facturatie__c(Accountname__c = testAccount.Name);
    insert a; 

    Attachment attach=new Attachment(); 
    attach.Name='Unit Test Attachment'; 
    Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body'); 
    attach.body=bodyBlob; attach.parentId=a.id;
    insert attach;

    List<Attachment> attachments=[select id, name from Attachment where parent.id=:a.id]; 
    System.assertEquals(1, attachments.size()); 
    
    PageReference pageRef = Page.Factuur2PDF;
    Test.setCurrentPage(pageRef);

     ApexPages.StandardController sc = new ApexPages.StandardController(a);
     attachPDF aPDF = new attachPDF (sc);
   

    }
}


 
This was selected as the best answer