0

I'm trying to update a lookup field on the lead object via trigger, but I'm not getting enough code coverage. Can someone help me ?

TRIGGER

trigger PartnerName on Lead (before insert) {
Map<string, List <Lead>> contactNamesToLeadsMap = new Map<string, List <Lead>>();

    for(Lead ld : trigger.new){
        if(!String.isBlank(ld.name)) {
            List<Lead> leadsForPartner = null;
            if(contactNamesToLeadsMap.containsKey(ld.name)) {
                leadsForPartner = contactNamesToLeadsMap.get(ld.name);
            } else {
                leadsForPartner = new List<Lead>();
                contactNamesToLeadsMap.put(ld.name, leadsForPartner);
            }
            leadsForPartner.add(ld);
        } else {
            ld.Partner_Name__c = null;
        }
    }

    List <Contact> contlist = [select Id, Name from Contact where Name in :contactNamesToLeadsMap.keySet()];

    for(Contact con : contlist) {


        List<Lead> leadsForPartner = contactNamesToLeadsMap.get(con.Name);
        for(Lead ld : leadsForPartner) {
            ld.Partner_Name__c = con.Id;
        }
    }
}

APEX CLASS

@IsTest
public class LeadPartnerTestClass {
    static testMethod Void LeadPartnerTestClass() {
        //Create an Account
        Account a = new account();
        a.Name = 'Test Account';
        insert a;

        //Create a Contact
        Contact c = new contact();
        c.FirstName = 'Test';
        c.LastName = 'Contact';
        c.AccountId = a.id;
        insert c;

        //Create a "Lead"
        Lead ld = new Lead();
        ld.FirstName= 'Test';
        ld.LastName = 'Contact';
        ld.Company = 'Test Account';
        ld.Partner_Name__c = c.id;
        ld.Status = 'Open';
        insert ld;

        //Query Success
        system.AssertEquals(c.id, ld.Partner_Name__c);
    }
}

Test Class

@IsTest
public class LeadPartnerTestClass {
    static testMethod Void LeadPartnerTestClass() {
        //Create an Account
        Account a = new account();
        a.Name = 'Test Account';
        insert a;

        //Create a Contact
        Contact con = new contact();
        con.FirstName = 'Test';
        con.LastName = 'Contact';
        con.AccountId = a.id;
        insert con;


        //Create a "Lead"
        Lead ld = new Lead();
        ld.FirstName= 'Test';
        ld.LastName = 'Lead';
        ld.Company = 'Test Account';
        ld.PartnerFirstName__c = 'Test';
        ld.PartnerLastName__c = 'Contact';
        ld.Partner_Name__c = con.id;
        ld.Status = 'Open';
        insert ld;

            //Query Success
        system.AssertEquals(con.id, ld.Partner_Name__c);
    }
}

![Code Coverage Screen Shot}(https://app.box.com/s/h2fnibjabfgpn8rvqg7m)

Updated Test Class

@IsTest
public class LeadPartnerTestClass {

    static testMethod Void LeadPartnerTestClass() {
        list<Lead> leadList = new list<Lead>();

        //Create an Account
        Account a = new account();
        a.Name = 'Test Account';
        insert a;

        //Create a Contact
        Contact con = new contact();
        con.FirstName = 'Test';
        con.LastName = 'Contact';
        con.AccountId = a.id;
        insert con;

        //Create a "Lead"
        Lead ld = new Lead();
        ld.FirstName= 'Test';
        ld.LastName = 'Lead';
        ld.Company = 'Test Account';
        ld.PartnerFirstName__c = 'Test';
        ld.PartnerLastName__c = 'Contact';
        ld.Partner_Name__c= con.id;
        ld.Status = 'Open';
        leadList.add(ld);

        Lead ld2 = new Lead();
        ld2.FirstName= 'Test';
        ld2.LastName = 'Lead2';
        ld2.Company = 'Test Account2';
        ld2.PartnerFirstName__c = null ;
        ld2.PartnerLastName__c = null ;
        ld2.Partner_Name__c= con.id;
        ld2.Status = 'Open';
        leadList.add(ld2);

        Lead ld3 = new Lead();
        ld3.FirstName= 'Test';
        ld3.LastName = 'Lead3';
        ld3.Company = 'Test Account3';
        ld3.PartnerFirstName__c = null ;
        ld3.PartnerLastName__c = null ;
        ld3.Status = 'Open';
        leadList.add(ld3);

        insert leadList;

         //Query Success
        system.AssertEquals(con.id, ld.Partner_Name__c);   
    }
}
2
  • What lines aren't getting coverage?
    – EricSSH
    Jan 9, 2015 at 17:18
  • I updated the code. Just to show the final outcome. It's getting 100% coverage now. Jan 9, 2015 at 17:46

2 Answers 2

2

Take a look at Testing Best Practices here, if you focus on use case as well as bulk record processing the code coverage will follow.

While only 75% of your Apex code must be covered by tests, your focus shouldn't be on the percentage of code that is covered. Instead, you should make sure that every use case of your application is covered, including positive and negative cases, as well as bulk and single records. This should lead to 75% or more of your code being covered by unit tests.

  • In the case of conditional logic (including ternary operators), execute each branch of code logic.

  • Make calls to methods using both valid and invalid inputs.

  • Complete successfully without throwing any exceptions, unless those errors are expected and caught in a try…catch block.

  • Always handle all exceptions that are caught, instead of merely catching the exceptions.

  • Use System.assert methods to prove that code behaves properly.

  • Use the runAs method to test your application in different user contexts.

  • Exercise bulk trigger functionality—use at least 20 records in your tests.

If you view which lines are not covered it should give you a better idea of what you are missing. Information on checking coverage can be found here, but as crmprogdev pointed out you need to make sure your tests enter each branch of your if-else statements. For example your test record has a name so the else(ld.Partner_Name__c = null;) in this statement(if(!String.isBlank(ld.name))) will not be covered. Another example would be this line if(contactNamesToLeadsMap.containsKey(ld.name)) will not be true when your test runs because you are only inserting one record so this line will not be covered either: leadsForPartner = contactNamesToLeadsMap.get(ld.name);

Checking Code Coverage

The Developer Console retrieves and displays code coverage information from your organization. Code coverage results come from any tests you’ve run from an API or from a user interface (for example, the Developer Console, the Force.com IDE, or the Apex Test Execution page). To clear the current results, click Test | Clear Test Data. When you edit a class, the code coverage for that class is cleared until you run the tests again.

You can view code coverage in several places in the Developer Console.

  • The Tests tab includes an Overall Code Coverage panel that displays the code coverage percentage for every Apex class in your
    organization that has been included in a test run. It also displays
    the overall percentage.

  • Double-click a completed test run to open a Tests Results view that displays the tested class, the tested method, the duration, result
    (skip, pass, or fail), and an optional error message. If the test
    failed, a Stack Trace column shows the method and line number at
    which the test failed.

  • To view line-by-line code coverage for an Apex class, open the class. The Code Coverage menu will include one or more of the following options depending on the tests you have implemented:

    -- None

    -- All Tests: The percentage of code coverage from all test runs.

    -- className.methodName: The percentage of code coverage from a method executed during a test run.

Lines of code that are covered by tests are blue. Lines of code that aren’t covered are red. Lines of code that don’t require coverage (for example, curly brackets, comments, and System.debug calls) are left white.

3
  • So I tried to update the test code but now i'm only getting 66% Covered. I'm struggling on this last part. I've added new test class and screenshot of covered code. Jan 6, 2015 at 15:57
  • @AndrewSims your test class didn't change too much really, still only a single insert test. In your test class you would need to create several leads in a list and insert that list. In that list you would want to make sure that at least 2 of the leads have the same name and that will help you reach line 8 of your trigger. Line 15 will only be hit if you insert a lead that has no name. Also in your new test you are setting the fields the trigger should be setting, you need to make it match your scenario. There is a contact "John Smith" and a new lead came in where Name = "John Smith".
    – Jenny B
    Jan 6, 2015 at 18:25
  • 1
    Thanks for all of the help. I was able to update test class to get 100% coverage. Jan 9, 2015 at 16:00
0

You need to create a separate method for each branch of your if-else statements or one method that has data in it that creates those respective conditions in order to get full coverage on your trigger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.