+ Start a Discussion
Mike DeMilleMike DeMille 

my test class not working

I have a simple trigger that updates a field.  My test class is not passing, however gives 80% coverage on the trigger.  

Here is the trigger:

trigger Update_MQL on Lead (before insert, before update) {
for (Lead myLead : Trigger.new)
{
    if (myLead.MQL_Date__c == null && (myLead.Status =='MQL (Marketing Qualified Lead)' || myLead.Status == 'SAL (Sales Accepted Lead)'))
    {myLead.MQL_Date__c = system.today();
    }else { myLead.MQL_Date__c = myLead.MQL_Date__c;
    }
     }
     
}


Here is the test class:

@isTest 
public class TestUpdate_MQL {
    static testMethod void insertNewLead() {
    
    Lead newLead = new Lead();
    
    newLead.FirstName = 'Mike';
    newLead.LastName = 'DeMille';
    newLead.Status = 'MQL (Marketing Qualified lead)';
    newLead.LeadSource = 'Advertisment';
    
    insert newLead;
    
 
       System.assertEquals(system.today(), newLead.MQL_Date__c);
    
    }
    
    }


What needs to be fixed?
Best Answer chosen by Mike DeMille
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest 
public class TestUpdate_MQL 
{
    static testMethod void insertNewLead() 
	{
    
		Lead newLead = new Lead();
		newLead.FirstName = 'Mike';
		newLead.LastName = 'DeMille';
		newLead1.Company = 'BlueWave';
		newLead.Status = 'MQL (Marketing Qualified lead)';
		newLead.LeadSource = 'Advertisment';
		insert newLead;

		List<Lead> lstLead = [ select MQL_Date__c from Lead where id =:newLead.id limit 1];
		
		
		System.assertEquals( system.today(), lstLead[0].MQL_Date__c );
		
		
		Lead newLead1 = new Lead();
		newLead1.FirstName = 'Mike';
		newLead1.LastName = 'DeMille';
		newLead1.Company = 'BlueWave';
		newLead1.Status = 'MQL (Marketing Qualified lead)';
		newLead1.LeadSource = 'Advertisment';
		newLead1.MQL_Date__c = system.today();
		insert newLead1;
		
	
    
    }
    
}

Please try above test class let us know if this will help you
 

All Answers

SequoyahSequoyah
It's failing because newLead.MQL_Date__c is null as far as the test is concerned. Your trigger sets the value of that field without the test "knowing" about it. You'll need to retrieve the lead with a SOQL query after it's inserted in order to allow the test to see the value of MQL_Date__c after the trigger has finished with it, and then run your assert on the retrieved record.
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest 
public class TestUpdate_MQL 
{
    static testMethod void insertNewLead() 
	{
    
		Lead newLead = new Lead();
		newLead.FirstName = 'Mike';
		newLead.LastName = 'DeMille';
		newLead1.Company = 'BlueWave';
		newLead.Status = 'MQL (Marketing Qualified lead)';
		newLead.LeadSource = 'Advertisment';
		insert newLead;

		List<Lead> lstLead = [ select MQL_Date__c from Lead where id =:newLead.id limit 1];
		
		
		System.assertEquals( system.today(), lstLead[0].MQL_Date__c );
		
		
		Lead newLead1 = new Lead();
		newLead1.FirstName = 'Mike';
		newLead1.LastName = 'DeMille';
		newLead1.Company = 'BlueWave';
		newLead1.Status = 'MQL (Marketing Qualified lead)';
		newLead1.LeadSource = 'Advertisment';
		newLead1.MQL_Date__c = system.today();
		insert newLead1;
		
	
    
    }
    
}

Please try above test class let us know if this will help you
 
This was selected as the best answer
Mike DeMilleMike DeMille
Thank you very much for the help! Both great help.