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

I need help with writing this test class ive started

Ive written this controller extension. 

Im attempting to write the test class for it but im not too sure how to write it for the particular class.

This is my class
public class CreatePDFAttachment {
    public String Id;
    public String Name;
    public String OrdId;

    public CreatePDFAttachment(ApexPages.StandardController controller) {
    Public pageReference attachment(){
    PageReference attachment = Page.InvCreated;
    //Attachment for Invoice Record
    Attachment e = new Attachment();
    //Attachment for Customer Order
    Attachment f = new Attachment();
    Blob body;
        body = attachment.getContentasPDF();
    catch(Exception t){
        body = Blob.valueOf('Cannot create PDF');

    // Attachment To Invoice__c
    e.body = body;
    e.Name = Name + ' - ' + system.Now();
    e.isPrivate = false;
    e.parentId = Id;
    e.ContentType = 'application/pdf';
    insert e;
    // Attachment To Customer_Order__c    
    f.body = body;
    f.Name = Name + ' - ' + system.Now();
    f.isPrivate = false;
    f.parentId = OrdId;
    f.ContentType = 'application/pdf';
    insert f;
    return new PageReference('/' + Id);


For the test class how would i get the references which im currently passing as apexpages.get parameters would i need to create the records in the class. This is what im at so far it doesnt fail but it does have 0 coverage> I thought it would have some sort of coverage.

This is my test class
private class TestCreatePDFAttachment {
    static testMethod void test1()
        String OrdId = 'a2Z5E000000ky1HUAQ';
        Invoice__c Inv = new Invoice__c(Customer_Order__c = OrdId); 
        insert Inv;
        Attachment attach=new Attachment();       
        attach.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        insert attach;
        List<Attachment> attachments=[select id, name from Attachment where];
        System.assertEquals(1, attachments.size());

Thank you in advance for any assistance.
AnudeepAnudeep (Salesforce Developers) 
Hi Josh, 

Test classes for controllers requires Instantiating a controller with all parameters in the page. Can you review the example listed in the following documentation and see if it helps?

Maybe you can try like this:

private class TestCreatePDFAttachment 

    static testMethod void test1()

        Account acc=new Account(Name='Acme Inc');
        insert acc;
        Attachment attach=new Attachment();       
        attach.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        insert attach;
            CreatePDFAttachment  obj = new CreatePDFAttachment();
            obj.inputId =;