+ Start a Discussion
tmbarrytmbarry 

Problem with a Test Class

Hello All, 

 

I am trying to write a test class for a trigger I created.

 

The trigger is a simple trigger:

trigger SD_Member_No_Contact_30_Days on Task (after update) {
for (Task t:System.Trigger.new)


{if (t.Subject == 'Lost Contact Follow Up Call' ){ 
    {if (t.IsClosed==True  && t.status<>'No More Calls This Cycle'  ){ 
    SD_Member__c SDM = New SD_Member__c (id=t.whatid, Lost_Contact_Follow_Up__c=True);
    Update SDM;
    }}}}
 
}

 When I write the test class, I only get 60% coverage:

@isTest
class TestClass_LostContact30Days {    
static testMethod void myUnitTest() {        
// TO DO: implement unit test       

// Create SD Member Record
SD_Member__c sd = New SD_Member__c(First_Name__c = 'John', 
                                   Last_Name__c = 'Smith', 
                                   Ins_ID_Member__c = '123456', 
                                   Ins_ID_Suscriber__c = '456789',
                                   address__c = '123 Main Stret', 
                                   city__c = 'Aster', 
                                   state__c = 'CT', 
                                   zip__c = '06001', 
                                   name = 'John Smith',
                                   Lost_Contact_Follow_Up__c =False);
insert sd;
                                 
// Test 1
Task o = new Task();
o.Subject = 'Lost Contact Follow Up Call'; //set the required fields
o.WhatId=sd.id;
o.Status='In Progress';
insert o;

o.Status='Complete';
update o;


}}

 With the explanation of:

 

trigger SD_Member_No_Contact_30_Days on Task (after update) {
for (Task t:System.Trigger.new)


{if (t.Subject == 'Lost Contact Follow Up Call' ){
{if (t.IsClosed==True && t.status<>'No More Calls This Cycle' ){
SD_Member__c SDM = New SD_Member__c (id=t.whatid, Lost_Contact_Follow_Up__c=True);
Update SDM;
}}}}

}

 

Any ideas?

 

Thanks Todd B.

 

Best Answer chosen by Admin (Salesforce Developers) 
SFAdmin5SFAdmin5

try this trigger and test class

 

trigger:

trigger SD_Member_No_Contact_30_Days on Task (after update) 
{
    for (Task t:System.Trigger.new)
            if (t.Subject == 'Lost Contact Follow Up Call' && t.IsClosed==True  && t.status<>'No More Calls This Cycle')
    { 
        SD_Member__c SDM = New SD_Member__c (id=t.whatid, Lost_Contact_Follow_Up__c=True);
        Update SDM;
        
    }
}

 

 

test class:

@isTest
    private class testTaskTrigger
    {
        static testMethod void verifySDMemberFieldSetter()
        {
        
        SD_Member__c sd = new SD_Member__c();
        sd.Name = 'test';
        sd.Lost_Contact_Follow_Up__c = false;
        insert sd;
        
        Task t = new Task();
        t.WhatId = sd.Id;
        t.Subject = 'Lost Contact Follow Up Call';
        t.Status = 'Completed';
        t.Priority = 'Normal';
        insert t;
        
        t.status = 'Completed';
        update t;
        
        List<SD_Member__c> retrieveSDMembers = [SELECT id,Lost_Contact_Follow_Up__c FROM SD_Member__c WHERE id =: sd.Id AND Lost_Contact_Follow_Up__c = true];
        
        Integer size = retrieveSDMembers.size();
        system.assertEquals(1,size);            

               
        }
    }

 

All Answers

SFAdmin5SFAdmin5

can you briefly explain what the trigger is supposed to accomplish?  thanks

colemabcolemab

Todd,

 

It looks like this if statement is returning false:

t.IsClosed==True && t.status<>'No More Calls This Cycle'

 

This indicate that most likely your test data doesn't meet both of these criteria.

 

Looking at your test data confirms this to be the case:

o.Status='In Progress';
insert o;

o.Status='Complete';
update o;

 

SFAdmin5SFAdmin5

try this trigger and test class

 

trigger:

trigger SD_Member_No_Contact_30_Days on Task (after update) 
{
    for (Task t:System.Trigger.new)
            if (t.Subject == 'Lost Contact Follow Up Call' && t.IsClosed==True  && t.status<>'No More Calls This Cycle')
    { 
        SD_Member__c SDM = New SD_Member__c (id=t.whatid, Lost_Contact_Follow_Up__c=True);
        Update SDM;
        
    }
}

 

 

test class:

@isTest
    private class testTaskTrigger
    {
        static testMethod void verifySDMemberFieldSetter()
        {
        
        SD_Member__c sd = new SD_Member__c();
        sd.Name = 'test';
        sd.Lost_Contact_Follow_Up__c = false;
        insert sd;
        
        Task t = new Task();
        t.WhatId = sd.Id;
        t.Subject = 'Lost Contact Follow Up Call';
        t.Status = 'Completed';
        t.Priority = 'Normal';
        insert t;
        
        t.status = 'Completed';
        update t;
        
        List<SD_Member__c> retrieveSDMembers = [SELECT id,Lost_Contact_Follow_Up__c FROM SD_Member__c WHERE id =: sd.Id AND Lost_Contact_Follow_Up__c = true];
        
        Integer size = retrieveSDMembers.size();
        system.assertEquals(1,size);            

               
        }
    }

 

This was selected as the best answer
SFAdmin5SFAdmin5

note i just revised the above trigger and test class.  it does what your trigger did and the test coverage is 100%

tmbarrytmbarry

Thanks Ross.  That worked great.  

 

If you have a few more minutes, could you explain what the last three lines of the test class do?

 

List<SD_Member__c> retrieveSDMembers = [SELECT id,Lost_Contact_Follow_Up__c FROM SD_Member__c WHERE id =: sd.Id AND Lost_Contact_Follow_Up__c = true];
        
        Integer size = retrieveSDMembers.size();
        system.assertEquals(1,size); 

 I quite sure what is happening there?

 

Thanks again.

SFAdmin5SFAdmin5

there i query the database for the sd member record i inserted earlier.  i want that record to have that custom field updated to true.  the size of the list should be 1.  after the list collection, i declare an integer variable, and assign it a value equal to the size of my list of records (again, 1).  then assert that that the integer (and therefore the list) size is 1.

 

if i were getting paid i'd write a real test that tests whether the trigger is bulkified or not, but i'm not, so i did the bare minimum to get you test coverage.