+ Start a Discussion
Travis WrightTravis Wright 

APEX Test Class Help

I am writing a test class for the below APEX trigger and running into something that I have never covered before and could use some help in understanding how to cover. 

trigger fillLeadSource on Opportunity (before update) {
   Set<Id> oppsToFill = new Set<Id>();
    for(Opportunity o : trigger.new){
        if(o.LeadSource == null) {
            oppsToFill.add(o.Id);
        }
    }

    // Now we'll select all possible contacts wasting only 1 query.
    if(!oppsToFill.isEmpty()){
        List<OpportunityContactRole> roles = [SELECT OpportunityId, Contact.Name, Contact.LeadSource, Contact.Lead_Source_Name__c
            FROM OpportunityContactRole
            WHERE isPrimary = true AND Contact.LeadSource != null AND OpportunityId IN :oppsToFill];

        if(!roles.isEmpty()){
            for(OpportunityContactRole ocr : roles){
                Opportunity oppToBeFilled = trigger.newMap.get(ocr.OpportunityId);
                System.debug('Changing lead source on ' + oppToBeFilled.Name + ' from ' + oppToBeFilled.LeadSource + ' to ' + ocr.Contact.LeadSource + ' (thx to ' + ocr.Contact.Name + ' being the Primary Contact).');
                oppToBeFilled.LeadSource = ocr.Contact.LeadSource;
                oppToBeFilled.Lead_Source_Name__c = orc.Contact.Lead_Source_Name__c;
            }
        }
    }
}


Lines I need help Covering 

            for(OpportunityContactRole ocr : roles){
                Opportunity oppToBeFilled = trigger.newMap.get(ocr.OpportunityId);
                System.debug('Changing lead source on ' + oppToBeFilled.Name + ' from ' + oppToBeFilled.LeadSource + ' to ' + ocr.Contact.LeadSource + ' (thx to ' + ocr.Contact.Name + ' being the Primary Contact).');
                oppToBeFilled.LeadSource = ocr.Contact.LeadSource;

TEST CLASS
@isTest
private class triggerfillLeadSource {
     
     static testMethod void fillLeadSource() {
     
        Account a = new Account();
        a.name = 'TNW (for Competitor Profile)';
        insert a;
        
        Contact c = new Contact();
        c.FirstName = 'Test';
        c.LastName = 'User 1';
        c.pi__score__c = 20;
        c.accountId = a.Id;
        c.LeadSource = 'Admin';
        insert c;
        
        c.pi__score__c = 30;
        update c;
        
        Delete c;
         
        Database.undelete(c.id);
        
        Asset ass = new Asset();
        ass.name = 'test Asset';
        ass.Price = 100;
        ass.Status = 'Purchased';
        ass.accountid = a.Id;
        insert ass;
        
        ass.price = 200;
        update ass;
        
        delete ass;
        
        Database.Undelete(ass.Id);
         
        Opportunity o = New Opportunity();
         o.Name = 'test';
         o.AccountId = a.Id;
         o.CloseDate = system.today();
         o.StageName = '0. Sales Ready';
         o.Type = 'New';
         o.LeadSource ='Admin';
         Insert o;
         
         OpportunityContactRole ocr = New OpportunityContactRole();
         ocr.opportunityId = o.Id;
         ocr.ContactId = c.Id;
         ocr.IsPrimary = True;
         insert ocr;
         
         Opportunity opp = [SELECT Leadsource,Name,ID from Opportunity where ID=:o.Id];
         System.assertEquals(c.LeadSource,opp.LeadSource);
       }
     }

 
PratikPratik (Salesforce Developers) 
Hi Travis,

You can refer to this:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm

Thanks,
Pratik
Travis WrightTravis Wright
Pratik,

Thanks for the information I have already looked at this but not understading all of it. I am still not sure how to cover the Map in the test Class. I think I am just making more of a mess but here is an update on what I have. 

@istest
private class testFillLeadSource {
    static testMethod void testFillLeadSource() {
    //Setup Account
    Account acc = new Account();
    acc.Name = 'Acme Account Test';
    acc.BillingCountry = 'US';
    acc.ShippingCountry = 'US';
    acc.BillingState = 'IL';
    acc.Website = 'www.acmeaccounttest.com';
    acc.Type = 'Prospect';
    acc.Current_Account_Status__c = 'Prospect';
    insert acc;
    
    //Setup Contact
    Contact con = new Contact();
    con.FirstName = 'Henry';
    con.LastName='Smith';        
    con.AccountId = acc.Id;
    con.LeadSource = 'test trigger';
    con.Lead_Source_Name__c = 'test trigger';
    con.Email = 'HSmith@acme.com';
    con.Phone = '1234567890';
    insert con;
    
    //Setup Opportunity
    Opportunity opp = new Opportunity();
    opp.AccountId = acc.Id;
    opp.Name = 'test lead source trigger';
    opp.StageName = '0. Sales Ready';
    opp.CloseDate = system.today();
    opp.Type = 'New';
    insert opp;
    
    //setup Opportunity Contact Role
    OpportunityContactRole ocr = new OpportunityContactRole();
    ocr.ContactId = con.Id;
    ocr.IsPrimary = True;
    ocr.OpportunityId = opp.Id;
    ocr.Role = 'Decision Maker';
    insert ocr;
          // Try to cover Map 
           Opportunity o = [Select Id, Name, StageName, Leadsource,
                        (Select Id, contact.name,contact.phone,contact.leadsource
                        from OpportunityContactRoles 
                        Where isPrimary = True And contact.LastName = 'Smith')
                        From Opportunity
                        Where Name = 'test lead source trigger'];
                
        list<OpportunityContactRole> ocr1 = [SELECT Id,OpportunityId,Contact.Name,Contact.LeadSource
                                                   from OpportunityContactRole
                                                   Where OpportunityID IN : o And isPrimary = true];        
        //Check final Match of Leadsource
        if(!ocr1.isEmpty()){
            Opportunity opptotest= Trigger.newMap.get(ocr1.OpportunityId);
            system.assert(o.Leadsource = ocr1.contact.LeadSource);
       
    }
  } 
}