function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TilluTillu 

How to acheive code coverage for this simple triggers?

I want to know how to write test classes to execute  the trigger code coverage? how should we write on what basis? for the trigger i have writtened some test classes but it wasnt giving code coverage for me. I almost executed all lines in trigger. where i am behind?

 

 

trigger createTaskForEmail on Task (before insert) {
   for (Task t : trigger.new) {
               if (t.status == 'Completed' && t.subject.startswith('Email:')) {
                          t.status = 'Completed';
                          t.description = t.description.substringAfter('Body:');
                          t.Activity_Direction__c = 'Email';
                          t.Activity_type__c='Email';
                }
     }
}

 

 

 

For this trigger I have writtened Test class some thing like below which gives code coverage up to 33 % only? what else i have left?

 

@isTest(SeeAllData = false)
private class Test_createTaskForEmail
{
static testMethod void UnitTestForException1()
{

  
Task ta = new task(Activity_type__c='Email'  , Activity_Direction__c='Email',status = 'completed');
insert ta ;
update ta ;

}
}

 

 

Like that i have other trigger i.e

 

trigger UpdateEventRating on Event (before insert, before update) {
for(Event E : Trigger.new){
if(E.Rating__c=='-Excellent Meeting (committed to business within 3 weeks)'){
E.Rating_Value__c=5;
}
else
if(E.Rating__c=='-Good Meeting (Resulted in client meeting agreed to/set or booked a seminar)'){
E.Rating_Value__c=4;
}
else
if(E.Rating__c=='-Productive Meeting (Client specific illustration requested or next meeting scheduled)'){
E.Rating_Value__c=3;
}
else
if(E.Rating__c=='-Average Meeting (Agreed to continued discussions, no client specific activity)'){
E.Rating_Value__c=2;
}
else
if(E.Rating__c=='-Poor Meeting (No potential for future business, no further activity needed)'){
E.Rating_Value__c=1;
}
}
}

 

 

Test Class For this trigger is this gives 53% code coverage only

 

@isTest(SeeAllData = false)

private class Test_UpdateEventRating 
{
static testMethod void UnitTestForException()
{
test.startTest();
Event e = new Event(ActivityDate = system.today(), ActivityDateTime = system.now(), DurationInMinutes = 2);
insert e ;
update e ;
test.stopTest();
}
}

 

Sagarika RoutSagarika Rout

The best way to write test classes is

 

1) populate the  data in test class  such a menner that , it will sattisfy the if else condition what you have written in code, so that autometicly it wiill cover the code coverage of your class.

2) If the trigger is written as  before insert / before update , then insert and update the data in respective test classes, so that it will sattisfy the trigger criteria and cover the code coverage.

 

I think you have missed to populate some data in your test classes.

 

 

 

Regards

Sagarika Rout

SFDC Developer

 

 

testrest97testrest97
@isTest(SeeAllData = false)
private class Test_createTaskForEmail
{
static testMethod void UnitTestForException1()
{

  
Task ta = new task(Activity_type__c='Email'  ,subject='Email:', Activity_Direction__c='Email',status = 'completed');
insert ta ;
update ta ;

}
}


@isTest(SeeAllData = false)

private class Test_UpdateEventRating 
{
static testMethod void UnitTestForException()
{
test.startTest();
Event e = new Event(ActivityDate = system.today(), ActivityDateTime = system.now(), DurationInMinutes = 2);
insert e ;
e.Rating__c='-Excellent Meeting (committed to business within 3 weeks)';
update e ;
e.Rating__c='-Good Meeting (Resulted in client meeting agreed to/set or booked a seminar)';
update e;
e.Rating__c='-Average Meeting (Agreed to continued discussions, no client specific activity)';
update e;
e.Rating__c='-Poor Meeting (No potential for future business, no further activity needed)';
update e;
test.stopTest();
}
}