You need to sign in to do that
Don't have an account?

66% code coverage in test class
Hi ,
I have an apex trigger as follows
trigger updateGroupLookup on Case (before insert) {
for (Case c : Trigger.new) {
if(c.groupid__c!= null){
c.group_Account__c = [select id from account where Group_ID__c=:c.groupid__c].ID;
}
}
}
The line marked in bold is not getting tested
I have written a test class for it as below.Please help.
@isTest
private class testUpdateLookup {
public static testMethod void testocc(){
Account acc = new Account(
name = 'temp validation account',
Type = 'Prospect',
Sub_Type__c='Merchant - Body',
BillingCity='Dubai',
BillingCountry='United Arab Emirates'
);
insert acc;
Account accCreated = [Select Name,group_id__c From Account Where Id =: acc.Id LIMIT 1];
Contact con=new Contact(
LastName='blah',
AccountId=accCreated.Id,
level__c='Other',
Role__c='Customer Support',
LeadSource='Consumer Referral'
);
insert con;
// TEST ADDING A NEW case
Case cas = new Case(
accountId = accCreated.id,
contactId=con.Id,
status='New',
Priority='Medium',
Type='Issue',
Origin='Email',
groupid__c=acc.group_id__c
);
insert cas;
//groupid null
Case cass = new Case(
accountId = accCreated.id,
contactId=con.Id,
status='New',
Priority='Medium',
Type='Issue',
Origin='Email'
);
insert cass;
}
}
I have an apex trigger as follows
trigger updateGroupLookup on Case (before insert) {
for (Case c : Trigger.new) {
if(c.groupid__c!= null){
c.group_Account__c = [select id from account where Group_ID__c=:c.groupid__c].ID;
}
}
}
The line marked in bold is not getting tested
I have written a test class for it as below.Please help.
@isTest
private class testUpdateLookup {
public static testMethod void testocc(){
Account acc = new Account(
name = 'temp validation account',
Type = 'Prospect',
Sub_Type__c='Merchant - Body',
BillingCity='Dubai',
BillingCountry='United Arab Emirates'
);
insert acc;
Account accCreated = [Select Name,group_id__c From Account Where Id =: acc.Id LIMIT 1];
Contact con=new Contact(
LastName='blah',
AccountId=accCreated.Id,
level__c='Other',
Role__c='Customer Support',
LeadSource='Consumer Referral'
);
insert con;
// TEST ADDING A NEW case
Case cas = new Case(
accountId = accCreated.id,
contactId=con.Id,
status='New',
Priority='Medium',
Type='Issue',
Origin='Email',
groupid__c=acc.group_id__c
);
insert cas;
//groupid null
Case cass = new Case(
accountId = accCreated.id,
contactId=con.Id,
status='New',
Priority='Medium',
Type='Issue',
Origin='Email'
);
insert cass;
}
}
All Answers
What is Group_ID__c hold??
You are inserting account without the value of Group_Id__c field value.
So Insert account with the corresponding field value called Group_Id__c. Then Check code coverage.
But one suggestion remove SOQL from for Loop.
group_id__c------autogenerated number on account
group_account__c----lookup field on case
100%
Can you please explain what was the problem so that i can learn to do this better?
with one case have null value of groupid__c and another case groupid__c have a value.. If both positive and negative get collapsed.
So do positive and negative test cases in different methods.