function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tom SimmonsTom Simmons 

Help with Test Class with wrappers

All, Im working on writing a test class for below controller however it keeps failing. This VF page allows to enter hierarchy of records in one click. When Im trying to test this, it keep failing and Im getting below error message when Im trying to call addAgendaComment () method. How should I increase my test coverage? Can some please help point me in right direction or give me an example?

Error: System.NullPointerException: Attempt to de-reference a null object
Class.AccountCasesCommentsEditExt.getCaseWrapperPos: line 36, column 1
Class.AccountCasesCommentsEditExt.getCaseWrapper: line 59, column 1
Class.AccountCasesCommentsEditExt.addCaseComment: line 154, column 1
Class.addMultpleTestClass2.testMyController: line 36, column 1

Wrapper 1
public with sharing class CaseCommentKeyWrapper 
{
    public Integer key {get; set;}
    public CaseComment comment {get; set;}

    public CaseCommentKeyWrapper(Integer inKey, CaseComment inComment)
    {
        key=inKey;
        comment=inComment;
    }
}

Wrapper 2:
public with sharing class CaseKeyWrapper 
{
    public Integer key {get; set;}
    public Case cs {get; set;}
    public List<CaseCommentKeyWrapper> comments {get; set;}
    private Integer commentKey=1;

    public CaseKeyWrapper(Integer inKey, Case inCs, List<CaseComment> inComments)
    {
        cs=inCs;
        key=inKey;
        comments=new List<CaseCommentKeyWrapper>();
        if (null!=inComments)
        {
            for (CaseComment cc : inComments)
            {
                comments.add(new CaseCommentKeyWrapper(commentKey++, cc));
            }
        }
    }
    public void addComment()
    {
        comments.add(new CaseCommentKeyWrapper(commentKey++, new CaseComment(ParentId=cs.id)));
    }
}

Controller Extension:
public with sharing class AccountCasesCommentsEditExt 
{
    public List<CaseKeyWrapper> caseWrappers {get; set;}

    public ApexPages.StandardController stdCtrl {get; set;}

    public Integer key=1;   

    public String caseToDel {get; set;}

    public String ccToDel {get; set;}

    public String caseToAddCC {get; set;}

    public List<Case> casesToDelete=new List<Case>();

    public List<CaseComment> commentsToDelete=new List<CaseComment>();

    public AccountCasesCommentsEditExt(ApexPages.StandardController std)
    {
        stdCtrl=std;
        List<Case> cases=[select id, Status, Subject, 
                          (select id, CommentBody, IsPublished, ParentId from CaseComments) 
                          from Case
                          where AccountId=:stdCtrl.getId()];    

        caseWrappers=new list<CaseKeyWrapper>();
        for (Case cs : cases)
        {
            caseWrappers.add(new CaseKeyWrapper(key++, cs, cs.CaseComments));
        }
    }

    public Integer getCaseWrapperPos(String keyStr)
    {
        Integer key=Integer.valueOf(keyStr.substring(2));
        Integer result=-1;

        Integer index=0;
        for (CaseKeyWrapper cand : caseWrappers)
        {
            if (cand.key==key)
            {
                result=index;
                break;
            }
            else
            {
                index++;
            }
        }

        return result;
    }

    public CaseKeyWrapper getCaseWrapper(String keyStr)
    {
        CaseKeyWrapper wrapper=null;
        Integer pos=getCaseWrapperPos(keyStr);
        if (-1!=pos)
        {
            wrapper=caseWrappers.get(pos);
        }

        return wrapper;
    }

    public Integer getCaseCommentWrapperPos(String keyStr, CaseKeyWrapper wrapper)
    {
        Integer key=Integer.valueOf(keyStr.substring(2));
        Integer result=-1;

        Integer index=0;
        for (CaseCommentKeyWrapper cand : wrapper.comments)
        {
            if (cand.key==key)
            {
                result=index;
                break;
            }
            else
            {
                index++;
            }
        }

        return result;
    }

    public CaseCommentKeyWrapper getCaseCommentWrapper(String keyStr, CaseKeyWrapper caseWrapper)
    {
        CaseCommentKeyWrapper wrapper=null;
        Integer pos=getCaseCommentWrapperPos(keyStr, caseWrapper);
        if (-1!=pos)
        {
            wrapper=caseWrapper.comments.get(pos);
        }

        return wrapper;
    }

    public PageReference deleteCase()
    {
        Integer pos=getCaseWrapperPos(caseToDel);
        if (-1!=pos)    
        {
            CaseKeyWrapper wrapper=caseWrappers.get(pos);
            if (null!=wrapper.cs.Id)
            {
                casesToDelete.add(wrapper.cs);
            }
            caseWrappers.remove(pos);
        }
        return null;
    }


    public PageReference deleteCaseComment()
    {
        String[] keyComps=ccToDel.split(':');

        Integer pos=getCaseWrapperPos(keyComps[0]);
        if (-1!=pos)    
        {
            CaseKeyWrapper wrapper=caseWrappers.get(pos);
            Integer commentPos=getCaseCommentWrapperPos(keyComps[1], wrapper);
            if (-1!=commentPos) 
            {
                CaseCommentKeyWrapper comWrap=wrapper.comments.get(commentPos);

                if (null!=comWrap.comment.Id)
                {
                    commentsToDelete.add(comWrap.comment);
                }
                wrapper.comments.remove(commentPos);
            }
        }

        return null;
    }

    public PageReference addCase()
    {
        caseWrappers.add(
            new CaseKeyWrapper(key++, 
                               new Case(AccountId=stdCtrl.getId()),
                               null));

        return null;
    }

    public PageReference addCaseComment()
    {
        CaseKeyWrapper wrapper=getCaseWrapper(caseToAddCC);
        if (null!=wrapper)
        {
            wrapper.addComment();
        }
        return null;
    }

    public PageReference save()
    {
        List<Case> cases=new List<Case>();
        for (CaseKeyWrapper wrapper : caseWrappers)
        {
            cases.add(wrapper.cs);
        }

        upsert cases;

        List<CaseComment> caseComments=new List<CaseComment>();
        for (CaseKeyWrapper wrapper : caseWrappers)
        {
            for (CaseCommentKeywrapper ccWrapper : wrapper.comments)
            {   
                CaseComment comment=ccWrapper.comment;
                if (null==comment.ParentId)
                {
                    comment.parentId=wrapper.cs.id;
                }
                caseComments.add(comment);
            }   
        }

        upsert caseComments;

        delete casesToDelete; 
        delete commentsToDelete; 

        return stdCtrl.save();
    }

}
My test class:
@isTest
public class addMultpleTestClass2 {

    public static testMethod void testMyController() {
        PageReference pageRef = Page.AccountCasesCommentsEdit;
        Test.setCurrentPage(pageRef);

        Account A = new Account ();    
        A.Name='Account Test'; 
        insert A;

        Case c = New Case ();
        c.Accountid = A.id;
        insert c;


        List<CaseComment> Casecomments = New   List <CaseComment>  ()    ;
        Casecomments.add(new CaseComment(CommentBody='test', Parentid=c.id));
        Casecomments.add(new CaseComment(CommentBody='test', Parentid=c.id));
        insert Casecomments;

        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(c);
        ApexPages.currentPage().getParameters().put('Id',c.Id);

        AccountCasesCommentsEditExt  controller = new AccountCasesCommentsEditExt (sc);

        controller.key  = 2; 
        controller.addCase();

        //I`m getting error at below line... 
        controller.addCaseComment();

    }
}


 
Best Answer chosen by Tom Simmons
Amit Singh 1Amit Singh 1
Hello,

To increase the coverage of your class you need to create test data as per the requirement means test records (Test records like those records you tested the functionality). For covering wrapper classes you need to create Instance of Wrapper class in your test class.

Also, I have made some changes and now your test class will pass without error but for code coverage you need to create test data.
 
@isTest
public class addMultpleTestClass2 {

    public static testMethod void testMyController() {
        PageReference pageRef = Page.AccountCasesCommentsEdit;
        Test.setCurrentPage(pageRef);

        Account A = new Account ();    
        A.Name='Account Test'; 
        insert A;

        Case c = New Case ();
        c.Accountid = A.id;
        insert c;


        List<CaseComment> Casecomments = New   List <CaseComment>  ()    ;
        Casecomments.add(new CaseComment(CommentBody='test', Parentid=c.id));
        Casecomments.add(new CaseComment(CommentBody='test', Parentid=c.id));
        insert Casecomments;

        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(c);
        ApexPages.currentPage().getParameters().put('Id',c.Id);

        AccountCasesCommentsEditExt  controller = new AccountCasesCommentsEditExt (sc);

        controller.key  = 2; 
        controller.addCase();

        //I`m getting error at below line...
        controller.caseToAddCC = '1024';
        controller.addCaseComment();

    }
}

Hope this helps :)

Thanks,
Amit Singh

All Answers

Amit Singh 1Amit Singh 1
Hello,

To increase the coverage of your class you need to create test data as per the requirement means test records (Test records like those records you tested the functionality). For covering wrapper classes you need to create Instance of Wrapper class in your test class.

Also, I have made some changes and now your test class will pass without error but for code coverage you need to create test data.
 
@isTest
public class addMultpleTestClass2 {

    public static testMethod void testMyController() {
        PageReference pageRef = Page.AccountCasesCommentsEdit;
        Test.setCurrentPage(pageRef);

        Account A = new Account ();    
        A.Name='Account Test'; 
        insert A;

        Case c = New Case ();
        c.Accountid = A.id;
        insert c;


        List<CaseComment> Casecomments = New   List <CaseComment>  ()    ;
        Casecomments.add(new CaseComment(CommentBody='test', Parentid=c.id));
        Casecomments.add(new CaseComment(CommentBody='test', Parentid=c.id));
        insert Casecomments;

        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(c);
        ApexPages.currentPage().getParameters().put('Id',c.Id);

        AccountCasesCommentsEditExt  controller = new AccountCasesCommentsEditExt (sc);

        controller.key  = 2; 
        controller.addCase();

        //I`m getting error at below line...
        controller.caseToAddCC = '1024';
        controller.addCaseComment();

    }
}

Hope this helps :)

Thanks,
Amit Singh
This was selected as the best answer
Amit Singh 1Amit Singh 1
Use Below code for Apex Class with 78% code coverage.
@isTest
public class addMultpleTestClass2 {

    public static testMethod void testMyController() {
    PageReference pageRef = Page.AccountCasesCommentsEdit;
    Test.setCurrentPage(pageRef);

    Account A = new Account ();    
    A.Name='Account Test'; 
    insert A;

    Case c = New Case ();
    c.Accountid = A.id;
    insert c;


    List<CaseComment> Casecomments = New   List <CaseComment>  ()    ;
    Casecomments.add(new CaseComment(CommentBody='test', Parentid=c.id));
    Casecomments.add(new CaseComment(CommentBody='test', Parentid=c.id));
    insert Casecomments;

    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(c);
    ApexPages.currentPage().getParameters().put('Id',c.Id);

    AccountCasesCommentsEditExt  controller = new AccountCasesCommentsEditExt (sc);

    TaskKeyWrapper Agendas = New TaskKeyWrapper (1, new Task(Whatid=a.id,payout__Category__c = 'Client Meeting',RecordTypeId='012A00000019sss'));
    CaseKeyWrapper caseWrapper = new CaseKeyWrapper(1,c, Casecomments);
    Test.StartTest();
    controller.key  = 2; 
    controller.addCase();
    controller.caseToAddCC = 'CS1';
    controller.caseToDel = 'CS2';
    controller.ccToDel='CS2:CC1'  ;
    controller.ccToDel='CS2:CC1'  ;
    String Key ='CS1';
    

    controller.addCaseComment();
    controller.deleteCase();
    controller.deleteCaseComment();
    controller.save();
    controller.getCaseCommentWrapperPos(Key,caseWrapper);
    controller.getCaseCommentWrapper(Key,caseWrapper);
    Test.stopTest();

}
}

Also, seems that below query is not returning any case record.
 
List<Case> cases=[select id, Status, Subject, 
                          (select id, CommentBody, IsPublished, ParentId from CaseComments) 
                          from Case
                          where AccountId=:stdCtrl.getId()];

Let me know if this helps and mark as best answer if this resolves your problem.

Thanks,
Amit Singh.
 
Tom SimmonsTom Simmons
Brilliant, it works. Thanks so much Amit. Last question, any ideas on how I can cover below lines? 


User-added image

User-added image
Amit Singh 1Amit Singh 1
To cover the lines you need to create exact test data in test class as per your exact condition and requirement.

As you are well aware that what your apex code does so you can create test data very easily.

Thanks,
Amit Singh
Tom SimmonsTom Simmons
Understood. Thanks very much Amit.