[SalesForce] Test Class coverage

I don't understand why the lines of my trigger marked with

Blockquote

aren't covered.
Please can you take a look?

TRIGGER:

trigger SetAllowPortalRegistration on Opportunity (before insert,before update) {

 Map<String, List<Opportunity>>  opportunitiesToUpdate = new Map<String, List<Opportunity>>();
 List<Contact> ContactsToUpdate=new List<Contact>();
        // ONLY IF THE FIELD IS CHANGED SLA_Expiry__c ??
        for (Opportunity opp : Trigger.new) 
        {
             if (opp.SLA_Expiry__c > date.today()){
                   if ((opportunitiesToUpdate.get(opp.AccountId) == null))
                    opportunitiesToUpdate.put(opp.AccountId, new List<Opportunity>());

                    opportunitiesToUpdate.get(opp.AccountId).add(opp);              
              }              
         }



            for(Contact co : [select firstname,lastname,CanAllowPortalSelfReg,ActiveContact__c From Contact WHERE accountid IN :opportunitiesToUpdate.keySet()])
                {
                          if (co.ActiveContact__c=='Yes'&& co.CanAllowPortalSelfReg!=true){
                                   co.CanAllowPortalSelfReg =true;
                                   ContactsToUpdate.add(co); 
                           }
                }
                if (ContactsToUpdate.size()>0)

update(ContactsToUpdate);

}

OLD TEST CLASS:
@isTest
private class AllowPortalRegistrationTest{

    static testMethod void AllowPortalRegistrationTest() {

    Account acct = new Account(name='test account');
    insert acct;
    Account acct2 = new Account(name='test');
    insert acct2;



    Opportunity opp1 = new Opportunity (Name='Opp1',StageName='Qualified',CloseDate=Date.today()+2, AccountId = acct2.id,LeadSource='Purchased database',MntRenewalDate__c = system.TODAY()+2);
    insert opp1;


    Opportunity opp2 = new Opportunity (Name='Opp2',StageName='Qualified',CloseDate=Date.today()+2, AccountId = acct.id, MntRenewalDate__c = system.TODAY()+3,LeadSource='Purchased database');
    insert opp2;


    Contact[] contactsToCreate = new Contact[]{};

    for(Integer x=0; x<200;x++){
        Contact ct = new Contact(AccountId=acct.Id,lastname='test',email='test@fff.com',ActiveContact__c='Yes',CanAllowPortalSelfReg=false);
        contactsToCreate.add(ct);

    }



     Contact[] contactsToCreatenoOpp = new Contact[]{};

    for(Integer x=0; x<200;x++){
        Contact ct = new Contact(AccountId=acct2.Id,lastname='test',email='test@ext.com',ActiveContact__c='Yes',CanAllowPortalSelfReg=false);
        contactsToCreate.add(ct);

    }

        Test.startTest();
         insert contactsToCreate;
         insert contactsToCreatenoOpp;     
         update contactsToCreatenoOpp;

        Test.stopTest();
    }

}

LATEST TEST CLASS:

@isTest
private class SetAllowPortalRegistrationTest{

        static testMethod void SetAllowPortalRegistrationTest() {



        Account acct = new Account(name='test account');
        insert acct;

        Opportunity opp1 = new Opportunity (Name='Opp1',StageName='Qualified', AccountId = acct.id,LeadSource='Purchased database',closedate=system.today(),MntRenewalDate__c =system.TODAY().addDays(4));
        insert opp1;



        Opportunity opp2 = new Opportunity (Name='Opp2',StageName='Qualified', AccountId = acct.id,LeadSource='Purchased database',MntRenewalDate__c = system.TODAY().addDays(4),closedate=system.today());
        insert opp2;


        Contact[] contactsToCreate = new Contact[]{};

        for(Integer x=0; x<200;x++){
            Contact ct = new Contact(AccountId=acct.Id,lastname='test',email='test@sss.com',ActiveContact__c='Yes',CanAllowPortalSelfReg=false);
            contactsToCreate.add(ct);

        }


             Test.startTest();  

             insert contactsToCreate;

             Opp1.name='newname';
             update Opp1;

            Test.stopTest();
        }

}

With the latest test class I get just also the row if (co.ActiveContact__c=='Yes'&& co.CanAllowPortalSelfReg!=true)covered by the test.

How can i cover the remanent line?

Thanks in advantage for any advice.

BR.

Best Answer

The problem here is that your trigger code is a trigger on Opportunities. It fires when you insert or update an opportunity.

In your test, you insert both your Opportunities before you have created any Contacts. So when the trigger runs, there are no Contacts for it to find.

At the block of code between Test.StartTest() and Test.stopTest() all you are doing is inserting and updating Contacts which doesn't fire the Opportunity trigger at all.

You probably should amend the code so that you do an update to Opportunity after the contacts have been set up, to test this part of the trigger. Unless you are expecting it to work when Contacts themselves are changed in which case you need a trigger directly on Contact.

Related Topic