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
sfdc-admin-smsfdc-admin-sm 

Help with writing a Test Class? + a question

Hello,

 

I need help with getting 100% coverage for my trigger below. Current code coverage is 37% (3/8)

 

trigger RunRules on Lead (After update) {
   string fname = UserInfo.getFirstName();
    for (Lead lead : Trigger.new) {
       if (fname == 'Fred' && (lead.L_Rate__c == 'A' ||lead.L_Rate__c == 'B') && LeadAssignRuleUtil.firstTime && Lead.ISConverted == False && Lead.OwnerID != '00G70000001ifuqFWE' ) {
            Database.DMLOptions dmo = new Database.DMLOptions();
            dmo.assignmentRuleHeader.useDefaultRule= true;
            lead.setOptions(dmo);
            LeadAssignRuleUtil.firstTime = false;
            Database.Update(lead);
       }
    }
}

 

My Question:

 

How do I know what Test Class is doing the code coverage for my Trigger? When I look under Setup -> Develop -> Apex Classes, I don't see any classes with a name close to or similar to the associated trigger name "RunRules" but I know it is getting partially covered (Current code coverage is 37%) by an unknown test class. 

gmb_power_devgmb_power_dev

Not sure if there is an easy way to identify the test(s) contributing to coverage, unless you run them all one by one.  But do remember that any apex test which updates lead records would cover this trigger partially.   By just looking at your trigger, it seems like you are missing coverage on the code within if loop.  To get 100% code coverage, you can write an explicit test which updates leads setting the data in such a way that the if loop is entered.  And of course, you want to assert on the changed behavior to make the test more meaningful, and name the test after the trigger (something like RunRulesTriggerTest) so it's easier for you in future to locate the test associated with the trigger.  Hope this helps.

sfdc-admin-smsfdc-admin-sm

Thanks for answering my question. It makes sense. For the test class problem, you are correct in saying that everything in the if statement needs to get covered.

 

The problem is I don't know how to cover it. Here is my test class:

 

@isTest(seealldata=true)
private class RunRules_Test {

string fname = UserInfo.getFirstName();


   static testMethod void myUnitTest() {
   
   Lead l = new Lead();
   l.firstname='test';
   l.lastname='testl';
   l.company='test';
   l.L_Rating__c = 'A';
   insert l;
   update l;
   
   LeadAssignRuleUtil obj = new LeadAssignRuleUtil();
   LeadAssignRuleUtil.firstTime = true;
   
   Database.DMLOptions dmo = new Database.DMLOptions();
   l.setOptions(dmo);

   test.startTest();
    
    update l;
    
    test.stopTest();
    
    
  }
  }

 Please give me specific code to achieve 100% code coverage.

 

Thanks.

k_bentsenk_bentsen

Try adding below at the top of your test method:

 

Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
User testU = new User(Alias = 'standt', FirstName = 'Fred', Email='standarduser@testorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
System.runAs(testU);

 

sfdc-admin-smsfdc-admin-sm
@k_bentsen. I added the code but the test coverage % remains the same. The following lines aren't covered:

Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;
lead.setOptions(dmo);
LeadAssignRuleUtil.firstTime = false;
Database.Update(lead);


Anything else?
k_bentsenk_bentsen
Have you tried looking at the debug log in the development console after you run your test class? It should help you identify why some lines of codes are not being executed.
sfdc-admin-smsfdc-admin-sm

@k_bentsen - I got this resolved based on your suggestion. Thanks for pointing me in the right direction.