0

I am trying to write test class for a class (I got help here with) to render a VF page in service console. Its just returning a list of all email attachments for a case. I dont know how to write a test class as its not performing any functionality just selecting a list.

Here is Class

Public Class AttachmentController
 {
    public List<Attachment> AttachedFiles {get; set;} //this is a way to share data between the controller and the page

    public AttachmentController(ApexPages.StandardController controller)
     { //this is where you can get the current case
      this.AttachedFiles = [SELECT Id, name FROM Attachment WHERE ParentId IN (SELECT Id FROM EmailMessage WHERE ParentId =:controller.getId())];
    }
  }

Here is test class I have which creates case, email and email attachment

@isTest
private class Attachment_TEST 
{

    static testMethod void TestAttachment()
    {

    Case caseObj = new Case(
    Status = 'New',
    Origin = 'Phone');

    insert caseObj;

    EmailMessage[] newEmail = new EmailMessage[0];
    newEmail.add(new EmailMessage(FromAddress = '[email protected]', Incoming = True, ToAddress= '[email protected]', Subject = 'Test email', TextBody = 'Test', ParentId = caseObj.Id));    
    insert newEmail;


        Attachment attach=new Attachment();     
        attach.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Test Attachment Body');
        attach.body=bodyBlob;
        attach.parentId=newEmail[0].id;
        insert attach;

        List<Attachment> attachments=[SELECT Id, name FROM Attachment WHERE ParentId IN (SELECT Id FROM EmailMessage WHERE ParentId =:caseObj.Id)];
        System.assertEquals(1, attachments.size());


    }
}

Can someone let me know how to somehow ensure its referring to my class

Thanks

2 Answers 2

0

This is how your test class should be. You were missing couple of things.

  1. Create Object of controller for which you're writing a test class.
  2. Use test.startTest() and test.stopTest() to specify beginning and ending to Test.

For writing test classes you can refer this Apex Test Class Trailhead

Code:

static testMethod void TestAttachment()
        {

              test.startTest();  
                Case caseObj = new Case(
                Status = 'New',
                Origin = 'Phone');

                insert caseObj;
                ApexPages.StandardController con = new ApexPages.StandardController(caseObj);
                AttachmentController at = new AttachmentController(con);
                EmailMessage[] newEmail = new EmailMessage[0];
                newEmail.add(new EmailMessage(FromAddress = '[email protected]', Incoming = True, ToAddress= '[email protected]', Subject = 'Test email', TextBody = 'Test', ParentId = caseObj.Id));    
                insert newEmail;


                    Attachment attach=new Attachment();     
                    attach.Name='Unit Test Attachment';
                    Blob bodyBlob=Blob.valueOf('Test Attachment Body');
                    attach.body=bodyBlob;
                    attach.parentId=newEmail[0].id;
                    insert attach;

                    List<Attachment> attachments=[SELECT Id, name FROM Attachment WHERE ParentId IN (SELECT Id FROM EmailMessage WHERE ParentId =:caseObj.Id)];
                    System.assertEquals(1, attachments.size());
            test.stopTest();

        }
1
  • @SLM3573 I'm glad that I could help. Mar 1, 2017 at 10:00
0

You need to instantiate the controller

1- Create the standard object

2- Create the standard controller based on the standard object

3- Create your custom controller based on the standard controller

StandardObject mySandardObject = new StandardObject();
ApexPages.StandardController myStandardController = new ApexPages.StandardController(mySandardObject);
AttachmentController myAttachmentController=new AttachmentController(controller);

After that you can do your asserts based on the myAttachementController

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .