0

All, I'm 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 I'm trying to test this, it keep failing and I`m getting below error message when I'm 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 Class 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 class 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);

    TaskKeyWrapper Agendas = New TaskKeyWrapper (1, new Task(Whatid=a.id,payout__Category__c = 'Client Meeting',RecordTypeId='012A00000019sss'));


    controller.key  = 2; 
    controller.addCase();
    controller.caseToAddCC = 'CS1';
    controller.caseToDel = 'CS2';
    controller.ccToDel='CS2:CC1'  ;
    controller.ccToDel='CS2:CC1'  ;

    controller.addCaseComment();
    controller.deleteCase();
    controller.deleteCaseComment();
    controller.save();

}

}enter image description here

  • 2
    You never set caseToAddCC. – Adrian Larson Feb 13 '17 at 8:16
  • Hi Adrian, sorry. I did not understand. Could you please elaborate with an example? What am I missing? – Tom Simmons Feb 13 '17 at 8:22
  • 1
    In your addCaseComment() method, you call the getCaseWrapper() method with caseToAddCC as parameter. But this variable is never set. It's null. This is your bug. – Martin Lezer Feb 13 '17 at 9:00
  • Thank you @MartinLezer and Chinmay. Very helpful.. that increase coverage. One more questions, I have updated my test class above and added an image of lines that are still not getting covered. Could you please point out why is that not getting covered? – Tom Simmons Feb 13 '17 at 10:21
  • You are rather missing the point of unit testing...You have not a single assertion in your test class. You should look at the relevant Trailhead modules and work your way through them. – Adrian Larson Feb 13 '17 at 10:25
-1

First of all by using your class name call to wrapper class & pass the parameters which is required for method.

i.e classname.wrapperclass cw = new classname.wrapperclass(parameters);

so that your wrapper along with method will cover.

| improve this answer | |
  • this is not good practise classname.wrapperclass cw = new classname.wrapperclass(parameters); wrapper class is calling itself in main class – cloudZigZag Feb 13 '17 at 12:38

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.