You need to sign in to do that
Don't have an account?
Sebastian Page
Hi All
Why Some line is not covering code coverage .
I am using test class here
@isTest
public class CaseMergeHandlerTest {
static testMethod void testMerge(){
List<String>DupCase =new List<string>();
Account testAccount = new Account(Name='Test Company Name123');
insert testAccount;
Contact con = new Contact(LastName='Testcont' ,Email='sss@test.com', AccountId=testAccount.id);
insert con;
list<case>lstcase =new LIst<case>();
Case caseObj1 = new Case();
caseObj1.AccountId = testAccount.Id;
caseObj1.contactId=con.id;
caseObj1.Status = 'Action-Automated Escalation';
caseObj1.Origin ='Email';
lstcase.add(caseObj1);
DupCase.add(caseObj1.id);
Case caseObj2 = new Case();
caseObj2.AccountId = testAccount.Id;
caseObj2.contactId=con.id;
caseObj2.Status = 'Closed Duplicate';
caseObj2.Origin ='Email';
lstcase.add(caseObj2);
DupCase.add(caseObj2.id);
test.startTest();
insert lstcase;
attachment attach=new attachment();
attach.name='test.html';
Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
attach.body=bodyBlob;
attach.parentId=lstcase[0].id;
insert attach;
List<Attachment> attachments=[select id, name from Attachment where parent.id=:lstcase[0].id];
System.assertEquals(1, attachments.size());
CaseMergeEmail.casAttachment(DupCase, caseObj1.id);
CaseMergeEmail.CasCommnet(DupCase, caseObj1.id);
test.stopTest();
}
}
Please help on this issue.
Why Some line of the code is not covering
Hi All
Why Some line is not covering code coverage .
I am using test class here
@isTest
public class CaseMergeHandlerTest {
static testMethod void testMerge(){
List<String>DupCase =new List<string>();
Account testAccount = new Account(Name='Test Company Name123');
insert testAccount;
Contact con = new Contact(LastName='Testcont' ,Email='sss@test.com', AccountId=testAccount.id);
insert con;
list<case>lstcase =new LIst<case>();
Case caseObj1 = new Case();
caseObj1.AccountId = testAccount.Id;
caseObj1.contactId=con.id;
caseObj1.Status = 'Action-Automated Escalation';
caseObj1.Origin ='Email';
lstcase.add(caseObj1);
DupCase.add(caseObj1.id);
Case caseObj2 = new Case();
caseObj2.AccountId = testAccount.Id;
caseObj2.contactId=con.id;
caseObj2.Status = 'Closed Duplicate';
caseObj2.Origin ='Email';
lstcase.add(caseObj2);
DupCase.add(caseObj2.id);
test.startTest();
insert lstcase;
attachment attach=new attachment();
attach.name='test.html';
Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
attach.body=bodyBlob;
attach.parentId=lstcase[0].id;
insert attach;
List<Attachment> attachments=[select id, name from Attachment where parent.id=:lstcase[0].id];
System.assertEquals(1, attachments.size());
CaseMergeEmail.casAttachment(DupCase, caseObj1.id);
CaseMergeEmail.CasCommnet(DupCase, caseObj1.id);
test.stopTest();
}
}
Please help on this issue.
It is most likely that query is returning no records, therefore your list.size() event does not equate to true.
regards
Andrew
Apex class is here-
public class CaseMergeEmail {
Public Static void casAttachment(List<String>dupCas ,String casId)
{
List<attachment> newAttachment =new list<attachment>();
Map<String, attachment> MapAttach= new Map<String,attachment>();
List<attachment> lstAttachments=[select id,Body,Name,ParentId from attachment where parentId in:dupCas];
if(lstAttachments!=null && !lstAttachments.isEmpty())
{
for(attachment att:lstAttachments)
{
attachment newattch=new attachment();
newattch.parentId=casId;
newattch.Body=att.body;
newattch.name=att.name;
//system.debug(' newattch.Body'+ newattch.Body);
//system.debug(' newattch.name'+ newattch.name);
newAttachment.add(newattch);
}
insert newAttachment;
}
}
Public Static void CasCommnet(List<String> dupCas,String casId){
list<caseComment> NewCaseCommet=new List<caseComment>();
list<caseComment> lstComment= [select id ,commentbody,parentid from casecomment where parentId in:dupCas];
if(lstComment.size()>0)
{
for(casecomment cmt:lstComment)
{ caseComment newcmt=new casecomment();
newcmt.ParentId=casId;
newcmt.CommentBody=cmt.CommentBody;
NewCaseCommet.add(newcmt);
}
}
insert NewCaseCommet;
}
public Static void TicketLogs(List<case>dupCas)
{
List<Spam_Ticket_Log__c> bigListData=new list<Spam_Ticket_Log__c>();
List<Case> CasLogLst=[select id ,Status,CaseNumber from Case Where Id In:dupCas];
if(CasLogLst.size()>0)
{
for(case cs:CasLogLst)
{
Spam_Ticket_Log__c cm=new Spam_Ticket_Log__c();
cm.Child_Ticket__c=cs.CaseNumber;
bigListData.add(cm);
}
}
if(bigListData.size()>0)
{
insert bigListData;
}
}
}
I did respond to this over the weekend, but my first response seems to have disappeared. So lets try again and see if i can recall what i wrote:
The issue is that the attachments will have nulls as their parent Id. Why? Because the Case records have not been inserted yet. On insertion, we get the Id. And the location in the code where you are populated the list of strings to hold the Ids, the values will be nulls. Therefore, that list of strings is populated with Nulls and therefore the select statement will return no values.
Update your test class as below:
I have also added some tweaks such as looping the Case creation to reduce the number of repetitive lines. I have moved the location of the Test.startTest() as it is better to have the actual code you are trying to test being isolated from the test data insertions. I also stuck in some asserts to add some assurance to your code.
Hope the above proves helpful
regards Andrew
p.s. All code provided as-is.