You need to sign in to do that
Don't have an account?
Ewa Fryst
help with Apex class test
Hello,
I have the following trigger on Case that defaults entitlements on Case record. I need to have 75% code coverage but i only managed to get 57% (found it on internet). Could anyone help me and amend the below test class to achieve the 75%?
Also, i don't really need the trigger to look at Contacts, only at Account level so that could be removed from the trigger if it is easier with writing the test class.
I would appreciate your help.
Ewa
The TRIGGER
trigger DefaultEntitlement on Case (Before Insert, Before Update) {
Set<Id> contactIds = new Set<Id>();
Set<Id> acctIds = new Set<Id>();
for (Case c : Trigger.new) {
contactIds.add(c.ContactId);
acctIds.add(c.AccountId);
}
List <Entitlement> entls = [Select e.StartDate, e.Id, e.EndDate,
e.AccountId, e.AssetId
From Entitlement e
Where e.AccountId in :acctIds And e.EndDate >= Today
And e.StartDate <= Today];
if(entls.isEmpty()==false){
for(Case c : Trigger.new){
if(c.EntitlementId == null && c.AccountId != null){
for(Entitlement e:entls){
if(e.AccountId==c.AccountId){
c.EntitlementId = e.Id;
if(c.AssetId==null && e.AssetId!=null)
c.AssetId=e.AssetId;
break;
}
}
}
}
} else{
List <EntitlementContact> entlContacts =
[Select e.EntitlementId,e.ContactId,e.Entitlement.AssetId
From EntitlementContact e
Where e.ContactId in :contactIds
And e.Entitlement.EndDate >= Today
And e.Entitlement.StartDate <= Today];
if(entlContacts.isEmpty()==false){
for(Case c : Trigger.new){
if(c.EntitlementId == null && c.ContactId != null){
for(EntitlementContact ec:entlContacts){
if(ec.ContactId==c.ContactId){
c.EntitlementId = ec.EntitlementId;
if(c.AssetId==null && ec.Entitlement.AssetId!=null)
c.AssetId=ec.Entitlement.AssetId;
break;
}
}
}
}
}
}
}
THE TEST CLASS
@isTest
private class DefaultEntitlementTest {
static testMethod void myUnitTest() {
Account acc = new Account(Name='testacc');
insert acc;
Contact con = new Contact(FirstName='john', LastName='doe', Email='john@doe.com', AccountId=acc.Id);
insert con;
Asset ass = new Asset(AccountId=acc.Id,ContactId=con.Id, Name='testing');
insert ass;
Entitlement ent = new Entitlement(Name='Testing', AccountId=acc.Id, StartDate=Date.valueof(System.now().addDays(-2)), EndDate=Date.valueof(System.now().addYears(2)), AssetId=ass.Id);
insert ent;
Entitlement ent2 = new Entitlement(Name='test2', AccountId=acc.Id, StartDate=Date.valueof(System.now().addDays(-10)), EndDate=Date.valueof(System.now().addYears(3)));
insert ent2;
EntitlementContact ec = new EntitlementContact(EntitlementId=ent.Id, ContactId=con.Id);
insert ec;
List<Case> listC = new list<Case>();
List<Id> newCaseIds = new List<Id>();
for(Integer i=0;i<20;i++){
Case c = new Case(ContactId=con.Id, AccountId=con.AccountId, Subject='Test Subject'+i, Origin='Webform_B2B');
listC.add(c);
}
test.startTest();
insert listC;
test.stopTest();
for (Case caseObj:listC) {
newCaseIds.add(caseObj.Id);
}
List<Case> entls = [Select EntitlementId, AssetId, ContactId, AccountId From Case Where Id in :newCaseIds];
for (Case caseObj:entls) {
System.debug(caseObj.Id + ' here are the values ' + caseObj.EntitlementId + ' and here ent.Id ' + ent.Id);
System.assertEquals(caseObj.EntitlementId, ent.Id);
}
Contact con2 = new Contact(FirstName = 'Jane',
LastName = 'Doe',
Email='jane@doe.com');
insert con2;
Entitlement ent3 = new Entitlement(Name='Testing', AccountId=acc.Id, StartDate=Date.valueof(System.now().addDays(2)), EndDate=Date.valueof(System.now().addYears(2)), AssetId=ass.Id);
insert ent3;
EntitlementContact ec2 = new EntitlementContact(EntitlementId=ent3.Id, ContactId=con2.Id);
insert ec2;
Case c1 = new Case(ContactId=con2.Id, AccountId=con.AccountId, Subject='Test Subject', Origin='Webform_B2B');
insert c1;
update c1;
}
}
I have the following trigger on Case that defaults entitlements on Case record. I need to have 75% code coverage but i only managed to get 57% (found it on internet). Could anyone help me and amend the below test class to achieve the 75%?
Also, i don't really need the trigger to look at Contacts, only at Account level so that could be removed from the trigger if it is easier with writing the test class.
I would appreciate your help.
Ewa
The TRIGGER
trigger DefaultEntitlement on Case (Before Insert, Before Update) {
Set<Id> contactIds = new Set<Id>();
Set<Id> acctIds = new Set<Id>();
for (Case c : Trigger.new) {
contactIds.add(c.ContactId);
acctIds.add(c.AccountId);
}
List <Entitlement> entls = [Select e.StartDate, e.Id, e.EndDate,
e.AccountId, e.AssetId
From Entitlement e
Where e.AccountId in :acctIds And e.EndDate >= Today
And e.StartDate <= Today];
if(entls.isEmpty()==false){
for(Case c : Trigger.new){
if(c.EntitlementId == null && c.AccountId != null){
for(Entitlement e:entls){
if(e.AccountId==c.AccountId){
c.EntitlementId = e.Id;
if(c.AssetId==null && e.AssetId!=null)
c.AssetId=e.AssetId;
break;
}
}
}
}
} else{
List <EntitlementContact> entlContacts =
[Select e.EntitlementId,e.ContactId,e.Entitlement.AssetId
From EntitlementContact e
Where e.ContactId in :contactIds
And e.Entitlement.EndDate >= Today
And e.Entitlement.StartDate <= Today];
if(entlContacts.isEmpty()==false){
for(Case c : Trigger.new){
if(c.EntitlementId == null && c.ContactId != null){
for(EntitlementContact ec:entlContacts){
if(ec.ContactId==c.ContactId){
c.EntitlementId = ec.EntitlementId;
if(c.AssetId==null && ec.Entitlement.AssetId!=null)
c.AssetId=ec.Entitlement.AssetId;
break;
}
}
}
}
}
}
}
THE TEST CLASS
@isTest
private class DefaultEntitlementTest {
static testMethod void myUnitTest() {
Account acc = new Account(Name='testacc');
insert acc;
Contact con = new Contact(FirstName='john', LastName='doe', Email='john@doe.com', AccountId=acc.Id);
insert con;
Asset ass = new Asset(AccountId=acc.Id,ContactId=con.Id, Name='testing');
insert ass;
Entitlement ent = new Entitlement(Name='Testing', AccountId=acc.Id, StartDate=Date.valueof(System.now().addDays(-2)), EndDate=Date.valueof(System.now().addYears(2)), AssetId=ass.Id);
insert ent;
Entitlement ent2 = new Entitlement(Name='test2', AccountId=acc.Id, StartDate=Date.valueof(System.now().addDays(-10)), EndDate=Date.valueof(System.now().addYears(3)));
insert ent2;
EntitlementContact ec = new EntitlementContact(EntitlementId=ent.Id, ContactId=con.Id);
insert ec;
List<Case> listC = new list<Case>();
List<Id> newCaseIds = new List<Id>();
for(Integer i=0;i<20;i++){
Case c = new Case(ContactId=con.Id, AccountId=con.AccountId, Subject='Test Subject'+i, Origin='Webform_B2B');
listC.add(c);
}
test.startTest();
insert listC;
test.stopTest();
for (Case caseObj:listC) {
newCaseIds.add(caseObj.Id);
}
List<Case> entls = [Select EntitlementId, AssetId, ContactId, AccountId From Case Where Id in :newCaseIds];
for (Case caseObj:entls) {
System.debug(caseObj.Id + ' here are the values ' + caseObj.EntitlementId + ' and here ent.Id ' + ent.Id);
System.assertEquals(caseObj.EntitlementId, ent.Id);
}
Contact con2 = new Contact(FirstName = 'Jane',
LastName = 'Doe',
Email='jane@doe.com');
insert con2;
Entitlement ent3 = new Entitlement(Name='Testing', AccountId=acc.Id, StartDate=Date.valueof(System.now().addDays(2)), EndDate=Date.valueof(System.now().addYears(2)), AssetId=ass.Id);
insert ent3;
EntitlementContact ec2 = new EntitlementContact(EntitlementId=ent3.Id, ContactId=con2.Id);
insert ec2;
Case c1 = new Case(ContactId=con2.Id, AccountId=con.AccountId, Subject='Test Subject', Origin='Webform_B2B');
insert c1;
update c1;
}
}
Your Single Test Method only tests one of the branches.
Create a second test the fulfills the entry requirement for the ELSE branch.
I would also look at using @TestSetup
something like:
regards
Andrew
All Answers
Your Single Test Method only tests one of the branches.
Create a second test the fulfills the entry requirement for the ELSE branch.
I would also look at using @TestSetup
something like:
regards
Andrew
Thanks for your response.
I'm not a developper so not sure how to change the test class with the ELSE branch. Or, shall i just use yours instead?
I tried but i got the error. Would you be able to correct it?
Many thanks
Ewa
There is a typo in the line - notice the dash which should be an equals
Contact con = [SELECT Id, AccountId FROM Contact WHERE Email ='john@doe.com' LIMIT 1];
as for how to do the test class, where there is a branch like IF..ELSE.. I find it better to produce 2 x test methods as shown in the rough example above.
you could simply use the two methods below - they should go close to coverng the trigger.
if you are feeling brave you could try redoing the above two methods with teh @testsetup tag,
HTH
regards
Andrew
Thanks for making the effort. I corrected the typo and run the test agaiin but it didn't increase the coverage at all.
I then tried the above code but i had an errror. Would you be able to fix it? I'm not a developer so any advise won't really help :-).
Many thanks
Ewa
so encapsulate the lot within a test method
regards
andrew
Ewa