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

How to write a test class for attachment?

public class ProcessSheet_MergeController {

    Date myDate = system.today();
    Private Process_Sheet__c pageOpp;
    public Boolean initialised{get; set;}
    public id PageOppId {get; set;} 
    public Process_Sheet__c getID {get; set;}
    public ProcessSheet_MergeController(ApexPages.StandardController controller) {
          this.pageOpp = (Process_Sheet__c)controller.getRecord();
         PageOppId = ApexPages.currentPage().getParameters().get('processsheetId');
        initialised=false;
        system.debug('id = ' + pageOpp.id + '     id2' +PageOppId);
      
    initialised=false;
    }
    public PageReference  savePDF(){
    getID = new Process_sheet__c();
    getID = [Select id,name, xParts__c,Part_ID__c from Process_Sheet__c where id=:PageOppId];     
    System.debug('!!!'+getID);      
    
       if (!initialised && getID.ID !=NULL) {
    System.debug('GetID'+getId.Id);
    //PageReference pdf = Page.ProcessSheet;
    PageReference pdf ; //= Page.ProcessSheet;
    pdf = new PageReference('/apex/ProcessSheet');
    pdf.getParameters().put('processsheetId',PageOppId);
    pdf.getParameters().put('PartProcessFieldset', 'ProcessSheetTemplate');
    pdf.getParameters().put('ProcessSheetFieldset', 'ProcessSheetTemplate');

    // create the new attachment
    List<Attachment> attachList = new List<Attachment>();
    Attachment attach = new Attachment();
    Blob body;

    try {
               body = pdf.getContentAsPDF();
        system.debug('body should be fine');

        } catch (VisualforceException e) {
            system.debug('in the catch block');
             body = Blob.valueOf('Some Text');
        }

    attach.Body = body;
    attach.Name = 'PSA_'+ getID.name +'.pdf' ;
    attach.IsPrivate = false;
    attach.ParentId = getID.Id;
    List<Attachment> a = [Select ID, ParentID, body, Name from Attachment Where ParentID = :getID.Id];
    delete a;
    
    attachList.add(attach);
    
    insert attachList;
    
    system.debug('<<>>'+attach);
    initialised=true;
} else system.debug('tried to run twice');
return new PageReference('/apex/pdfMerge?processsheetId='+getID.id+'&PartProcessFieldset=ProcessSheetTemplate&ProcessSheetFieldset=ProcessSheetTemplate');

  public ProcessSheet_MergeController (){}
}
Steven NsubugaSteven Nsubuga
@isTest
private class ProcessSheet_MergeControllerTest  {

	@isTest
    static void processSheetMergeControllerTest() {
        process_sheet__c p = new process_sheet__c(part_id__c = 'something');
		insert p;
		
		Blob b = Blob.valueOf('Test Attachment');
		
		Attachment att = new Attachment();
		att.ParentId = p.Id;
		att.name = 'Test Attachment' ;
		att.Body = b;
		insert att;
		
		
        PageReference pageRef = Page.ThePage; // Add your VF page Name here
		Test.setCurrentPage(pageRef);
		
		pageRef.getParameters().put('processsheetId', String.valueOf(p.Id));
		
		ApexPages.StandardController sc = new ApexPages.StandardController(p);
		ProcessSheet_MergeController processSheetMergeController = new ProcessSheet_MergeController(sc);

		System.assert(processSheetMergeController.PageOppId != null);
		System.assert(processSheetMergeController.initialised == false);
		System.assert(processSheetMergeController.savePDF() != null);
    }
}