0

I have a trigger that takes the Converted Contact and updates a custom contact lookup field on the Converted Opportunity with that Contact. This trigger is working fine, but I am having trouble creating the test class. The current coverage is 46% and I need 100%. Looking for any suggestions on how to increase this coverage.

Trigger

Trigger VennSci_Lead_Convert on Lead (After update) {

    if (Trigger.isAfter) {

        if (Trigger.isUpdate) { 

            Map<ID,ID> oppToContact = new Map<ID,ID>(); 

            for (Lead ld : Trigger.new) {
                if(ld.isConverted && trigger.oldMap.get(ld.id).isConverted) continue; //only process newly converted leads

                // Find all converted Leads with Opportunitiy and add ConvertedOpportunityId to setConvertedOppIds
                if (ld.ConvertedOpportunityId != null && ld.ConvertedContactId != null){                    
                    oppToContact.put(ld.ConvertedOpportunityId,ld.ConvertedContactID);  

                }

            }  

       if (!oppToContact.isEmpty()) {

                List<Opportunity> lstOpp = new List<Opportunity>();
                for(Opportunity opps:[select Id,AccountId,CampaignId from Opportunity where Id in :oppToContact.keySet()]){

                    if(oppToContact.containsKey(opps.id)){
                        opps.Sourced_By__c = oppToContact.get(opps.id);
                        lstOpp.add(opps);
                    }

                }


        if (!lstOpp.isEmpty()) {
          Update lstOpp;
         }

    }
}}}

Test Class

@IsTest (SeeAllData=true) private class VennSci_TEST_LeadConvert{

    /* This is a basic test which simulates the primary positive case for the 
       Conversion method of a Lead. */

private static testMethod void myUnitTest() {

// create a Lead
Lead lead=new Lead(LastName='Doe',FirstName='John',Company='Test',Status='Converted');
insert lead;                


lead=  [SELECT Id,Status, FirstName,LastName,Company,LeadSource
             FROM Lead
             WHERE Id = :lead.Id];

    System.assertEquals('Converted', lead.Status);

   }
   }
1
  • You do not have a Contact or an Opportunity associated with your lead. In the trigger, you are checking that ld.ConvertedOpportunityId != null && ld.ConvertedContactId != null. Since you have no Opp or contact added to your lead in test code, your map is empty. You need to add a contact and an opportunity in your test code – Chris Duncombe May 23 '14 at 14:57
9

Nevermind, I figured it out. I wasn't sure how to set my lead to convert in the test but here is the finished product at 100% coverage.

@IsTest (SeeAllData=true) private class VennSci_TEST_LeadConvert{

    /* This is a basic test which simulates the primary positive case for the 
       Conversion method of a Lead. */

private static testMethod void myUnitTest() {

// create a Lead
Lead lead=new Lead(LastName='Doe',FirstName='John',Company='Test',Status='Inquiry');

insert lead;                

Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(lead.id);
lc.setDoNotCreateOpportunity(false);
lc.setConvertedStatus('Converted');

Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
}}
5
  • You should probably remove the (SeeAllData=true) from your test class since a test like this shoudln't require it. – dphil May 23 '14 at 16:11
  • @dphil I am fairly new at development and all the examples I saw had SeeAllData = TRUE. What is the reason you would have it and what is the reason I don't need it? – Grace May 23 '14 at 18:06
  • 2
    @Grace When not specified, your test is isolated from most data in your org which is a very good thing as tests need to work in more than one org. That does mean you need to add the data yourself in the test which is more typing but at least then the data that is set up and the asserts on the results are next to one another. SeeAllData=true is an anti-pattern; see articles like SeeAllData=true - Why I Think You Shouldn't Use It for more detail. – Keith C May 23 '14 at 18:51
  • @KeithC thanks for the explanation, that makes alot of sense – Grace May 23 '14 at 20:22
  • Thanks mate for the Answer. I'm struggling with it from 2 days and you have given the perfect code snippet that works. Just carefully pluged in the Status field and got 100%.. – user11815 Sep 23 '14 at 10:20

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.