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
GarrettzGarrettz 

Simple Trigger Test - I'm getting 0% coverage

I'm in the process of learning how to develope with Apex and I have a question about a test I'm trying to write for a trigger. I'm finding both don't have any syntax errors or anything but the test has 0% coverage of the trigger. The trigger is pretty basic, based on a status (I have a query for this) update a lookup field (rs_BM__c) with the same record as another lookup field (Enrollment_Manager__c) on the same table (Lead). Here is my Trigger code:

 

trigger SetBMForTransition on Lead (after update, after insert) {
	for(Lead a : [SELECT Id, Associate_Status__c, rs_BM__c from Lead
					WHERE (Associate_Status__c = 'Pending'
					OR Associate_Status__c='Enrolled' )
					AND rs_BM__c = null] )
					{a.rs_BM__c=a.Enrollment_Manager__c;}
}

 Here is my test code:

@isTest
private class SetBMForTransitionTriggerTest {

    static testMethod void pendingAndNull() {

		//instantiate the data
        List<Lead> inquiries = new List<Lead>();
        My_Biz_Office_User__c a = new My_Biz_Office_User__c();
        a.name='Sara Conde';
        My_Biz_Office_User__c b = new My_Biz_Office_User__c();
        b.name='Nicolette Taylor';
        insert a;
        insert b;	
        for (Integer i = 1; i <=20; i++){
        	inquiries.add(new Lead(FirstName='Test'
        						  ,LastName='Inquiry' + i
        						  ,Enrollment_Manager__c=b.id
        					      ,Status='Inquiry'));
        }
        for(Lead newInquiry : [SELECT id, name, Enrollment_Manager__c, Status
        					   FROM Lead
        					   WHERE Id
        					   IN :inquiries]){
					      		  	insert(newInquiry);
        					   }

		//update the data
        for(Lead change : [SELECT id, name, Enrollment_Manager__c, rs_BM__c, Status 
        				   FROM Lead
        				   WHERE Id
         				   IN :inquiries]){
        					  	change.Status='Pending';
      	}
        for(Lead newInquiry : [SELECT id, name, Enrollment_Manager__c, Status
        					   FROM Lead
        					   WHERE Id
        					   IN :inquiries]){
					      		  	update(newInquiry);
        					   }

		//test the data
        for(Lead updated : [SELECT id, name, Enrollment_Manager__c, rs_BM__c 
        					FROM Lead
        					WHERE Id
        					IN :inquiries]){
                				System.assertEquals(updated.rs_BM__c,updated.Enrollment_Manager__c);
    	}
}
}

 Thanks in advance - any help would be appreciated

SLockardSLockard

For your trigger, you don't have to query the leads because you already have access to the records by using Trigger.New, Trigger.newMap, Trigger.oldMap ...

And in your test class, you need to insert them after you make them, and you don't have to query them again before updating.

Try something like this:

trigger SetBMForTransition on Lead (before update, before insert)
{
	for (Lead a : Trigger.New)
	{
		if ((a.Associate_Status__c == 'Pending' || a.Associate_Status__c == 'Enrolled') && a.rs_BM__c == null)
		{
			a.rs_BM__c = a.Enrollment_Manager__c;
		}
	}
}

@isTest
private class SetBMForTransitionTriggerTest 
{

    static testMethod void pendingAndNull() 
	{

		//instantiate the data
        List<Lead> inquiries = new List<Lead>();
        My_Biz_Office_User__c a = new My_Biz_Office_User__c();
        a.name='Sara Conde';
        My_Biz_Office_User__c b = new My_Biz_Office_User__c();
        b.name='Nicolette Taylor';
        insert a;
        insert b;	
        for (Integer i = 1; i <=20; i++)
		{
        	inquiries.add(new Lead(FirstName='Test'
			  ,LastName='Inquiry' + i
			  ,Enrollment_Manager__c=b.id
			  ,Status='Inquiry'));
        }
		insert inquiries;
		//update the data
        for(Lead change : inquiries)
		{
			change.Status='Pending';
      	}
		update inquiries;

		//test the data
        for(Lead updated : [SELECT id, name, Enrollment_Manager__c, rs_BM__c 
			FROM Lead
			WHERE Id
			IN :inquiries]){
				System.assertEquals(updated.rs_BM__c,updated.Enrollment_Manager__c);
    	}
	}
}

 

GarrettzGarrettz

Thanks SLockard,

 

Thanks for cleaning up some of the code. It's working (partially), I have 66% coverage now.

 

The piece that is failing is the System.assertEquals on the test class. It's throwing the following error: System.AssertException: Assertion Failed: Expected: null, Actual: a09c0000000WfBbAAK

 

I would have thought the line:

a.rs_BM__c = a.Enrollment_Manager__c

 

Would have updated the BM field with the value from the Enrollment Manager field and the assertion would be testing to make sure that is true - is this not the case?