I've set up an apex class (below) and visualforce page to allow uploading of custom attachments. It seems to work fine in the sandbox. Now, I'm having trouble testing the apex class so I can deploy in production org. Any help (guidance) with writing test for the apex class would be very much appreciated.

public class UploadAttachmentController {

    public String selectedCAorMA {get;set;}
    public Boolean selectedCAProcessed {get;set;}
    public Boolean selectedMAProcessed {get;set;}
    public String description {get;set;}
    private Refugee__c refugee {get;set;} 
    public String fileName {get;set;}
    public Blob fileBody {get;set;}

    public UploadAttachmentController(ApexPages.StandardController controller) { 
        this.refugee = (Refugee__c)controller.getRecord();

    // creates a new Refugee_Attachment__c record
    private Database.SaveResult saveCustomAttachment() {
        Refugee_Attachment__c obj = new Refugee_Attachment__c();
        obj.refugee__c = refugee.Id; 
        obj.description__c = description;
        obj.CA_or_MA__c = selectedCAorMA;
        obj.CA_Processed__c = selectedCAProcessed;
        obj.MA_Processed__c = selectedMAProcessed;
        // fill out cust obj fields
        return Database.insert(obj);

    // creates an actual Attachment record with the Refugee_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;
        // insert the attachment
        result = Database.insert(attachment);
        // reset the file for the view state
        fileBody = Blob.valueOf(' ');
        return result;

    * Upload process is:
    *  1. Insert new Refugee_Attachment__c record
    *  2. Insert new Attachment with the new Refugee_Attachment__c record as parent
    *  3. Update the Refugee_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
                Refugee_Attachment__c customAttachment = [select id from Refugee_Attachment__c where id = :customAttachmentResult.getId()];
                customAttachment.name = this.fileName;
                customAttachment.Attachment__c = attachmentResult.getId();
                update customAttachment;

        } catch (Exception e) {
            return null;

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

    public PageReference back() {
        return new PageReference('/'+Refugee.Id);


Test Class

public class UploadAttachmentControllerTest {
    static testMethod void testUploadAttachmentController() {
    UploadAttachmentController controller = new UploadAttachmentController
    controller.description = 'Test description'
    controller.selectedCAorMA = 'CA'
  • I guess that's what you are looking for – Novarg May 31 '16 at 14:16
  • Hi there. Here ate stackexchange we do not write code for you. We give you pointers on how you can improve your own code. Did you try anything yourself already? – Nielsm May 31 '16 at 14:16
  • 2
    Hi Deli, welcome to SFSE! Please take a look at the help center and How to Ask. This question is likely to get closed as off topic, since you do not have an attempt to share with us. If you do a little basic research and attempt to write some tests on your own, I am sure you can come back to us with more specific problems you are having and they will be well received. – Adrian Larson May 31 '16 at 14:19
  • Yes, i've been searching and trying a few things. Since I'm new to writing apex tests, i'm having trouble getting started. There's a lot of information out there and I'm trying to sort through it all. – DEli May 31 '16 at 14:19
  • Here is where I'm starting at for the test: – DEli May 31 '16 at 15:17

A good starting point may be the salesforce testing best practices guide. You can find it here: Best practices

Some very important points

  • Always create your own test data. Make sure that your tests run in production and other sandboxes as well.
  • Write tests for both positive flows (if all goes right) as for negative flows (if something goes wrong)
  • Use assertions to make sure that the code als does what you expect it to do.
| improve this answer | |

public Blob fileBody {get;set;} should be set as a transient variable. If persistent between transactions it will cause your viewstate to grow too large.

| improve this answer | |
  • Did you mean for this to be a comment? – Adrian Larson May 31 '16 at 14:15
  • crmprogdev, thanks for the feedback. Unfortunately, i'm still pretty new to writing apex and don't understand what you mean. Can you elaborate? Suggested fix? – DEli May 31 '16 at 14:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.