You need to sign in to do that
Don't have an account?
Tom 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
Wrapper 2:
Controller Extension:
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(); } }
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.
Hope this helps :)
Thanks,
Amit Singh
All Answers
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.
Hope this helps :)
Thanks,
Amit Singh
Also, seems that below query is not returning any case record.
Let me know if this helps and mark as best answer if this resolves your problem.
Thanks,
Amit Singh.
As you are well aware that what your apex code does so you can create test data very easily.
Thanks,
Amit Singh