1

I am trying to get test coverage for a method in a controller extension. As part of my "Pagereference Save()" method, I am creating an attachment and trying to insert it with a try/catch block as shown below. Addtionally, I have added the getAttachedFile method I have created from earlier in the class:

public Attachment getAttachedFile(){
    attachedFile = New Attachment();
    return attachedFile;

}

public pagereference Save() {

Attachment a = new Attachment(ParentId = supportCase.Id, name = attachedFile.Name, body = attachedFile.Body);    

        try{
        insert a; 
        }
        catch(DMLException except){
            String mg = except.getMessage();
            if (mg.contains('Body') && mg.contains('Name')){
                isThereFile = false;
            }
            else{ 

            ApexPages.addMessage(New ApexPages.Message(ApexPages.severity.Error, 'It seems there was an issue with you attachment. Please ensure that it is below the file size limit of 25 MB'));
            PageReference pageRef = ApexPages.currentPage();
              return pageRef;
            }
}

In my test class, in one of the test methods, I create a case, insert it, and additionally run both methods from the controller as shown below:

Test.startTest();

    Account testAccount = new Account(Name = 'testingAccount');
    insert testAccount;

    Case cs = new Case(accountId = testAccount.Id, subject='test', Protocol_Issue__c = 'requred issue', Protocol_Sub_Issue__c = 'reuired sub issue', SuppliedPhone = '1111111111', suppliedEmail= 'testemail@testttest.net', SuppliedName = 'Tim Test', Description = 'testDescription' );
    insert cs;

    ApexPages.StandardController ctlr   = new ApexPages.StandardController(cs);
    ControllerExtension controller = new ControllerExtension(ctlr);


    PageReference createdObject = controller.Save();

    cs.SuppliedPhone = '1234567891123y';

    Attachment attach = controller.getAttachedFile();

    Test.StopTest();

Is it possible to change or append the test method so that the Attachemnt code in the "Save()" method gets covered?

1

You can refactor save() method as follows not only get the coverage, but also for actual testing.

public Integer attachmentBodyLength =0;

public pagereference Save() {
Attachment a = new Attachment(ParentId = supportCase.Id, name = attachedFile.Name, body = attachedFile.Body);   

try{
    if(Test.isRunningTest()){
        attachmentBodyLength = 10; //any length you can put which can be tested
    }
    else{
        attachmentBodyLength = 26214400; //25MB, Salesforce limit
    }
    System.debug('a.BodyLength=' + a.BodyLength);
if(a.BodyLength>attachmentBodyLength)
{
    throw new myCustomException('It seems there was an issue with you attachment. Please ensure that it is below the file size limit of 25 MB');
}

insert a;
return null; 
}
catch(DMLException except)
    {
        String mg = except.getMessage();
        System.debug(mg);
        if (mg.contains('Body') && mg.contains('Name'))
        {
            isThereFile = false;                
        }
        return null;
    }            
 catch(Exception e)
   {
      ApexPages.addMessage(New ApexPages.Message(ApexPages.severity.Error, e.getMessage()));
      PageReference pageRef = ApexPages.currentPage();
      return pageRef;
   }
}
} 

Now from your test class create 2 scenarios for testing the insert attachment exception.

  1. Create an attachment record with either name or body as null. And during insertion it will throw the DMLException.

  2. Create an attachment with bodyLength greater that attachmentBodyLength (which has been specified in Controller) and it will throw CustomException, which will be caught in the Exception block.

This approach just came to my mind to get rid of creating 25MB attachments.

Refer apex testing best practices for writing test classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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