+ Start a Discussion
JoshTonksJoshTonks 

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(){
    Id=ApexPages.currentPage().getParameters().get('Id');
    Name=ApexPages.currentPage().getParameters().get('Name');
    OrdId=ApexPages.currentPage().getParameters().get('OrdId');
    
    PageReference attachment = Page.InvCreated;
    attachment.getParameters().put('id',Id);
    attachment.getParameters().put('name',Name);
    attachment.getParameters().put('OrdId',OrdId);
    
    //Attachment for Invoice Record
    Attachment e = new Attachment();
    
    //Attachment for Customer Order
    Attachment f = new Attachment();
    Blob body;
    try{
        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
 
@isTest
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');
        attach.IsPrivate=false;
        attach.body=bodyBlob;
        attach.parentId=Inv.id;
        insert attach;
        
        List<Attachment> attachments=[select id, name from Attachment where parent.id=:Inv.id];
        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? 

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm

Maybe you can try like this:

@isTest
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');
        attach.IsPrivate=false;
        attach.body=bodyBlob;
        attach.parentId=acc.id;
        insert attach;
        
        Test.startTest();
            
            CreatePDFAttachment  obj = new CreatePDFAttachment();
            obj.inputId = acc.id;
            obj.attachment();
        
        Test.stopTest();    
        
    }
}

Anudeep