+ Start a Discussion
Arul Bernard I 14Arul Bernard I 14 

How to write a test class for attachment

Here is my code,
public class pdfMerge_Controller {

public List<Attachment> att{get;set;}
public List<Attachment> attch{get;set;}

    public pdfMerge_Controller(ApexPages.StandardController controller) {
        String partid;
        String proId;
         id ProcessId = ApexPages.currentPage().getParameters().get('processsheetId');
    proId = [select id,part_id__c from process_sheet__c where id=:ProcessId].Id;
    partid = [select id,part_id__c from process_sheet__c where id=:ProcessId].part_id__c ;
    set<Id> Ids = new set<Id>();
    Ids.Add(partid);
    Ids.Add(proId);
    att=[Select a.Id,a.ContentType,a.ParentId,a.Parent.Type,a.Parent.Name,a.BodyLength From Attachment a where  a.parent.Id IN  :Ids AND name like 'PSA_%' ];  
    

    
    }

}
Best Answer chosen by Arul Bernard I 14
Steven NsubugaSteven Nsubuga
Try this
@isTest
private class pdfMerge_Controller {

	@isTest
    static void pdfMergeControllerTest() {
        process_sheet__c p = new process_sheet__c(part_id__c = 'something');
		insert p;
		
		Blob b = Blob.valueOf('Test Data');
		
		Attachment att = new Attachment();
		att.ParentId = p.Id;
		att.name = 'PSA_something' ;
		att.Body = b;
		insert att;
		
		
        PageReference pageRef = Page.ThePage; // Add your VF page Name here
		Test.setCurrentPage(pageRef);
		
		ApexPages.StandardController sc = new ApexPages.StandardController(p);
		pdfMerge_Controller pdfMergeController = new pdfMerge_Controller(sc);

		System.assert(pdfMergeController.att != null);
    }
}