0

I wrote test class for a trigger and covered 72%. I jst want to know how to cover Trigger.oldmap.get area.

if(oppObj.Key_Negotiator__c ==null && Trigger.oldMap.get(oppObj.Id).Key_Negotiator__c !=null && OppContactRoleMap.get(oppObj.id).size() < 4)
            {

                 Opportunity OldoppObj=Trigger.oldMap.get(oppObj.Id);     
                 system.debug('--OldoppObj-->>'+OldoppObj);    
                 OppId.add(OldoppObj.id);
                 ContactId.add(OldoppObj.Key_Negotiator__c); 

            }
            else if(oppObj.Key_Negotiator__c!=null && Trigger.oldMap.get(oppObj.Id).Key_Negotiator__c==null)
            {

                newContactRoleList.add(new OpportunityContactRole (ContactId=oppObj.Key_Negotiator__c, OpportunityId=oppObj.Id, Role='Key Negotiator'));
            }

Test class:

@isTest(seealldata=true)
public class TestCreateContactRole {
    static testmethod void UpdateAccountSubscriptionEmail()
    {   
        Account a = new account();
        a.name='test';
        insert a;

        contact con =new contact();
        con.LastName='Test';
        con.AccountId=a.Id;
        insert con;

        opportunity opp=new opportunity();
        opp.AccountId=a.Id;
        opp.name='test';
        opp.StageName= 'Lead';
        opp.CloseDate= System.today(); 
        opp.Contact_Person__c=con.Id;
        opp.Key_Negotiator__c=con.Id;
        opp.Design_Offices__c=a.Id;
        opp.Partner_in_Charge__c=con.Id;
        opp.Project_Design_Leader__c=con.Id;


        insert opp;
        //update opp;

        //Test.starttest();
        opportunity opp1=new opportunity();
        opp1.AccountId=a.Id;
        opp1.id=opp.id;
        opp1.name='test';
        opp1.StageName= 'Lead';
        opp1.CloseDate= System.today(); 
        opp1.Contact_Person__c=con.Id;
        opp1.Key_Negotiator__c=con.Id;
        opp1.Design_Offices__c=a.Id;
        opp1.Partner_in_Charge__c=con.Id;
        opp1.Project_Design_Leader__c=con.Id;
        update opp1;
        //Test.stopTest();
    }



}

enter image description here

3
  • Your question would be much easier to answer if you posted your existing test class and told us which lines of code in trigger aren't covered (mark them in your code as we can't tell). Since Trigger.oldmap.get(oppObj.Id) is used throughout your trigger, without seeing your existing unit test code, one might conclude that you've not written one at all.
    – crmprogdev
    Nov 8, 2015 at 19:47
  • Hi @crmprogdev I updated test class. Nov 8, 2015 at 19:55
  • Best practice suggestion - remove SeeAllData=true - this will only lead to ruin somewhere down the line during a deployment
    – cropredy
    Nov 9, 2015 at 19:47

2 Answers 2

1

You need to have two test methods. In essence, you need to create another one for where the Key_Negotiator__c==null. You already have one for where the Key_Negotiator__c!=null. Leave that field empty when creating data in the new method and you should have what you need.

After inserting your opportunities, your test should have a:

test.startTest();
// lines where you make changes to the opportunity records
update opp;

test.stopTest();

// query opp to get new values
// system.asserts that new values are as expected

To help support your test requirements, you may need to add additional contacts.

0

In order to get test coverage, like crmprogdev said, you should have separate unit tests to test various scenarios.

Now in order to get into that other section, it looks like Key_Negotiator__c must originally be null and then moving to a non-null value.

2 ways to do this:

1) Remove/comment this line:

opp.Key_Negotiator__c = con.Id;

2) Null out the value before you do anything else after your insert:

insert opp;
opp.Key_Negotiator__c = null;
update opp;

Test.starttest();
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .