Skip to main content
Hello All,

I am sure you see requests all the time to aid in the creation, modification and testing of new APEX code from greenhorns like myself. The code below is complete and helps with a custom attachment object that replaces the standard Notes & Attachments section on Contracts. The code, object and process works great in the Sandbox environment but I am having difficulties testing the code.

I have browsed through the forums looking and have been researching online for best practices on testing APEX coding in general and have been unable to tackle the project fully. If anyone could assist, I would greatly appreciated it!

APEX Code Testing - UploadAttachmentController

public class UploadContractAttachmentController {

public String selectedType {get;set;}

public String description {get;set;}

private Contract contract {get;set;}

public String fileName {get;set;}

public Blob fileBody {get;set;}

public UploadContractAttachmentController(ApexPages.StandardController controller) {

this.contract = (Contract)controller.getRecord();

}

// creates a new Contract_Attachment__c record

private Database.SaveResult saveCustomAttachment() {

Contract_Attachment__c obj = new Contract_Attachment__c();

obj.contract__c = contract.Id;

obj.description__c = description;

obj.type__c = selectedType;

// fill out cust obj fields

return Database.insert(obj);

}

// create an actual Attachment record with the Contract_Attachment__c as parent

private Database.SaveResult saveStandardAttachment(Id parentId) {

Database.SaveResult result;

Attachment attachment = new Attachment();

attachment.body = this.fileBody;

attachment.name = this.fileName;

attachment.parentId = parentId;

// inser the attahcment

result = Database.insert(attachment);

// reset the file for the view state

fileBody = Blob.valueOf(' ');

return result;

}

/**

* Upload process is:

* 1. Insert new Contract_Attachment__c record

* 2. Insert new Attachment with the new Contract_Attachment__c record as parent

* 3. Update the Contract_Attachment__c record with the ID of the new Attachment

**/

public PageReference processUpload() {

try {

Database.SaveResult customAttachmentResult = saveCustomAttachment();

if (customAttachmentResult == null || !customAttachmentResult.isSuccess()) {

ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,

'Could not save attachment.'));

return null;

}

Database.SaveResult attachmentResult = saveStandardAttachment(customAttachmentResult.getId());

if (attachmentResult == null || !attachmentResult.isSuccess()) {

ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,

'Could not save attachment.'));

return null;

} else {

// update the custom attachment record with some attachment info

Contract_Attachment__c customAttachment = [select id from Contract_Attachment__c where id = :customAttachmentResult.getId()];

customAttachment.name = this.fileName;

customAttachment.Attachment__c = attachmentResult.getId();

update customAttachment;

}

} catch (Exception e) {

ApexPages.AddMessages(e);

return null;

}

return new PageReference('/'+contract.Id);

}

public PageReference back() {

return new PageReference('/'+contract.Id);

}

}

 
12 respuestas
  1. 13 ene 2015, 8:05
    Hi David, try the following test class

     

    @isTest

    public class UploadContractAttachmentController_Tests {

    @isTest static void testProcessUpload(){

    // set the page

    test.setCurrentPageReference(page.UploadContractAttachment);

    //create the contract here

    contract c = new contract();

    c.Status = 'Draft';

    c.StartDate = system.today();

    c.ContractTerm = 12;

    insert c;

    ApexPages.StandardController sc = new ApexPages.StandardController(c);

    UploadContractAttachmentController ucac = new UploadContractAttachmentController(sc);

    //fill page variables

    ucac.selectedType = 'VALUE';

    ucac.description = 'VALUE';

    ucac.fileName = 'FILE NAME';

    ucac.fileBody = blob.valueOf('abcd');

    test.startTest();

    //click the button

    ucac.processUpload();

    test.stopTest();

    }

    }

     
0/9000