You need to sign in to do that
Don't have an account?
Tom Simmons
Help with code coverage on Class
All, below is my code coverage. It allows me update hierarchy of records at one time. Code works fine however I`m not currently able to get full 100 % code coverage. What I have right now is about 92 % and I dont understand what`s missing? Can someone of you pls point it out? or maybe give me a sample test class that would work?
Wrapper Class:
Extension:
Test Class:
Lines not being covered,
Wrapper Class:
public with sharing class TaskKeyWrapper { public Integer key {get; set;} public Task comment {get; set;} public TaskKeyWrapper(Integer inKey, Task inComment) { key=inKey; comment=inComment; } }Wrapper Class:
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))); } }
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); } } try { upsert caseComments; delete casesToDelete; delete commentsToDelete; } catch(exception e) { } return stdCtrl.save(); } }
Test Class:
@isTest public class addMultpleTestClass2 { public static testMethod void testMyController() { Account A = new Account (Name='Account Test'); insert A; Case c1 = New Case (Accountid = A.id); insert c1; Case c2 = New Case (Accountid = A.id); insert c2; CaseComment cc1 = New CaseComment (CommentBody='test',Parentid=c1.id); insert cc1; CaseComment cc2 = New CaseComment (CommentBody='test',Parentid=c1.id); insert cc2; List<CaseComment> Casecomments = New List <CaseComment> () ; Casecomments.add(cc1); Casecomments.add(cc2); Test.StartTest(); PageReference pageRef = Page.AccountCasesCommentsEdit; Test.setCurrentPage(pageRef); ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(A); ApexPages.currentPage().getParameters().put('Id',A.Id); AccountCasesCommentsEditExt controller = new AccountCasesCommentsEditExt (sc); TaskKeyWrapper Agendas = New TaskKeyWrapper (1, new Task(Whatid=c1.id)); CaseKeyWrapper caseWrapper = new CaseKeyWrapper(1,c1, Casecomments); 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.getCaseWrapperPos (key); controller.getCaseCommentWrapperPos(Key,caseWrapper); controller.getCaseCommentWrapper(Key,caseWrapper); controller.save(); Key ='CS2'; controller.getCaseCommentWrapperPos(Key,caseWrapper); controller.save(); Test.stopTest(); } }
Lines not being covered,
Here is the Test Class that gives you 99% coverage.
I also have figured out that few lines of code in your AccountCasesCommentsEditExt class cannot be covered because ParentId of a CaseComment could never be null.
Here is the updated class
This gives you 100% coverage.
Let me know how it goes.
All Answers
Your code above seems to be missing the definition for CaseCommentKeyWrapper. Can you please share that too?
Here is the Test Class that gives you 99% coverage.
I also have figured out that few lines of code in your AccountCasesCommentsEditExt class cannot be covered because ParentId of a CaseComment could never be null.
Here is the updated class
This gives you 100% coverage.
Let me know how it goes.