You need to sign in to do that
Don't have an account?
Allen2
There is an apex class. I have written test class on this which covered only 46% code. Could anyone please help me to cover 100% code coverage.
Apex Class
trigger updateDetails on WLog__c (after update)
{
List<id> caseIds = new List<id>();
List<id> ParentCaseids = new List<id>();
List<case> parentchildlist = new List<case>();
List<case> updcaselist=new List<case>();
Set<String> gbS = SMCaseUtils.getSalesOrgs('GB');
if(trigger.isAfter && trigger.isUpdate)
{
for(WLog__c olog : trigger.new)
{
if(olog.Result__c == 'Success' && olog.Date_Issued__c != null)
caseIds.add(olog.Case__c);
}
system.debug('case ids from olog -->'+caseIds);
if(!caseIds.isEmpty()){
for(Case cs :[select id, ParentId, SV_Parent_Case__c from case where id in :caseIds AND Reasons__c = 'System Generated' AND Org_From_IP__c in :gbS]){
if(cs.SV_Parent_Case__c){
ParentCaseids.add(cs.id);
}else
{
if(cs.parentId!=null){
ParentCaseids.add(cs.ParentId);
}else
{
ParentCaseids.add(cs.id);
}
}
}
}
system.debug('parent ids list -->'+ParentCaseids);
if(!ParentCaseids.isEmpty()){
parentchildlist=[select id,Reasons__c,CC_Overview_Status__c,(select id,CC_Completed_On__c from Cases) from case where id in :ParentCaseids];
}
system.debug('parent child list -->'+parentchildlist);
for(Case parent : parentchildlist)
{
Boolean isDateNull=false;
for(Case child : parent.cases)
{
if(child.CC_Completed_On__c==null)
{
isDateNull=true;
break;
}
}
if(isDateNull==false)
{
parent.CC_Overview_Status__c='Process Complete';
updcaselist.add(parent);
}
}
if(!updcaselist.isEmpty())
{
system.debug('inside update for case list in olog-->'+updcaselist);
update updcaselist;
}
}
}
Test class
@isTest(seeAllData=true)
public class updateDetailsTest
{
static testMethod void ParentCaseTestMethod1()
{
Test.startTest();
List<WLog__c> ologlist =[select id,Case__c,Result__c,Date_Issued__c from WLog__c limit 1];
if(!ologlist.isEmpty())
{
ologlist[0].Date_Issued__c=system.today();
ologlist[0].Result__c='Success';
update ologlist;
}
Test.stopTest();
}
static testMethod void ParentCaseTestMethod()
{
Test.startTest();
Set<String> gbS = SMCaseUtils.getSalesOrgs('GB');
List<Case> cs=[select id, ParentId, SV_Parent_Case__c,CCDetails__c from case where
Reasons__c = 'System Generated' AND Org_From_IP__c in :gbS AND CCDetails__c!=null
limit 1];
if(!cs.isEmpty())
{
List<WLog__c> ologlist =[select id,Case__c,Result__c,Date_Issued__c from WLog__c
where Case__c=:cs[0].CCDetails__c limit 1];
if(!ologlist.isEmpty())
{
ologlist[0].Date_Issued__c=system.today();
ologlist[0].Result__c='Success';
update ologlist;
}
}
Test.stopTest();
}
}
The underlined bold italic part of the apex class not covered in the test class please help me out..
trigger updateDetails on WLog__c (after update)
{
List<id> caseIds = new List<id>();
List<id> ParentCaseids = new List<id>();
List<case> parentchildlist = new List<case>();
List<case> updcaselist=new List<case>();
Set<String> gbS = SMCaseUtils.getSalesOrgs('GB');
if(trigger.isAfter && trigger.isUpdate)
{
for(WLog__c olog : trigger.new)
{
if(olog.Result__c == 'Success' && olog.Date_Issued__c != null)
caseIds.add(olog.Case__c);
}
system.debug('case ids from olog -->'+caseIds);
if(!caseIds.isEmpty()){
for(Case cs :[select id, ParentId, SV_Parent_Case__c from case where id in :caseIds AND Reasons__c = 'System Generated' AND Org_From_IP__c in :gbS]){
if(cs.SV_Parent_Case__c){
ParentCaseids.add(cs.id);
}else
{
if(cs.parentId!=null){
ParentCaseids.add(cs.ParentId);
}else
{
ParentCaseids.add(cs.id);
}
}
}
}
system.debug('parent ids list -->'+ParentCaseids);
if(!ParentCaseids.isEmpty()){
parentchildlist=[select id,Reasons__c,CC_Overview_Status__c,(select id,CC_Completed_On__c from Cases) from case where id in :ParentCaseids];
}
system.debug('parent child list -->'+parentchildlist);
for(Case parent : parentchildlist)
{
Boolean isDateNull=false;
for(Case child : parent.cases)
{
if(child.CC_Completed_On__c==null)
{
isDateNull=true;
break;
}
}
if(isDateNull==false)
{
parent.CC_Overview_Status__c='Process Complete';
updcaselist.add(parent);
}
}
if(!updcaselist.isEmpty())
{
system.debug('inside update for case list in olog-->'+updcaselist);
update updcaselist;
}
}
}
Test class
@isTest(seeAllData=true)
public class updateDetailsTest
{
static testMethod void ParentCaseTestMethod1()
{
Test.startTest();
List<WLog__c> ologlist =[select id,Case__c,Result__c,Date_Issued__c from WLog__c limit 1];
if(!ologlist.isEmpty())
{
ologlist[0].Date_Issued__c=system.today();
ologlist[0].Result__c='Success';
update ologlist;
}
Test.stopTest();
}
static testMethod void ParentCaseTestMethod()
{
Test.startTest();
Set<String> gbS = SMCaseUtils.getSalesOrgs('GB');
List<Case> cs=[select id, ParentId, SV_Parent_Case__c,CCDetails__c from case where
Reasons__c = 'System Generated' AND Org_From_IP__c in :gbS AND CCDetails__c!=null
limit 1];
if(!cs.isEmpty())
{
List<WLog__c> ologlist =[select id,Case__c,Result__c,Date_Issued__c from WLog__c
where Case__c=:cs[0].CCDetails__c limit 1];
if(!ologlist.isEmpty())
{
ologlist[0].Date_Issued__c=system.today();
ologlist[0].Result__c='Success';
update ologlist;
}
}
Test.stopTest();
}
}
The underlined bold italic part of the apex class not covered in the test class please help me out..
if(!caseIds.isEmpty()){
for(Case cs :[select id, ParentId, SV_Parent_Case__c from case where id in :caseIds AND Reasons__c = 'System Generated' AND Org_From_IP__c in :gbS]){
if(cs.SV_Parent_Case__c){
ParentCaseids.add(cs.id);
}else
{
if(cs.parentId!=null){
ParentCaseids.add(cs.ParentId);
}else
{
ParentCaseids.add(cs.id);
}
}
}
}
The correct way to do it is Insert Test Data.
Insert Data for this method (SMCaseUtils.getSalesOrgs('GB'))
Insert Data for Case Object and pass the Id to Data Insertion of Wlog__c
Insert Data for WLog__c with Result__c = 'Success' and olog.Date_Issued__c != null
This Will probably give you more than 75% Coverage
If you are still not clear then I can Probably help you with the Code