1

I'm trying to get proper code coverage for a schedulable class, but I'm only hitting the 45% mark and I'm not sure why. Here's the class:

global class opportunityContactReferralIdCopy implements Schedulable {

   global void execute(SchedulableContext SC) {

        // Create a map
        Map<ID,String> updateTheseOpps = new Map<ID,String>();

        // Create a loop with SOQL and add results to the list
        for(OpportunityContactRole ocr : [ SELECT OpportunityId,Contact.Referral_ID__c 
                                           FROM OpportunityContactRole 
                                           WHERE ContactId IN (SELECT Id FROM Contact WHERE Referral_ID__c != null) 
                                           AND OpportunityId IN (SELECT Id FROM Opportunity WHERE Referral_ID__c = null) 
                                           AND CreatedDate = TODAY]) {

            updateTheseOpps.put(ocr.OpportunityId,ocr.Contact.Referral_Id__c);

        }

        // Determine the size of the list
        Integer ListSize = updateTheseOpps.size();

        if( ListSize < 1 ) {

            system.debug('List is too small');

        } else {

            // List is a proper size
            system.debug('List is big enough');

            // Update needs to know the type so create objects of the right type
            Opportunity[] updates = new Opportunity[] {};

            // Loops and creates the update
            for (Id id : updateTheseOpps.keySet()) {

                updates.add(new Opportunity(Id = id, Referral_Id__c = updateTheseOpps.get(id)));

            }

            update updates;

        }

    }

}

And the test class:

@isTest
private class opportunityContactReferralIdCopyTest {

    static testMethod void test2() {    

        Test.StartTest();

        Account a          = new Account(Name='TestAccount');
        insert a;
        String aId = a.id;

        Contact c          = new Contact(AccountId=a.Id,FirstName='Bob',LastName='Smith',LeadSource='Advertising',Lead_Source_Detail__c='PPC');
        insert c;
        String cId = c.id;

    Opportunity o         = new Opportunity(Name='FakeOp',
                                                      Type='New Business',
                                                      StageName='Nurture',
                                                      CloseDate=system.today()+90,
                                                      Competitors__c='None',
                                                      ACV__c=decimal.valueof('20'),
                                                      AccountId=a.Id
                                                     );
        insert o;
        String oId = o.id;

        OpportunityContactRole ocr   = new OpportunityContactRole(ContactId=cId,OpportunityId=oId,Role='Champion');
        insert ocr;

        opportunityContactReferralIdCopy sh1 = new opportunityContactReferralIdCopy();

        String sch = '0 0 23 * * ?'; 

        system.schedule('TestOppCopy', sch, sh1); 

        Test.stopTest();

    }
}

Any ideas why this wouldn't achieve proper coverage?

3

Well, certainly you're not getting inside your else block, because your inner join looks for Contact records where Referral_ID__c != null. Notice this clause in your query:

WHERE ContactId IN (SELECT Id FROM Contact WHERE Referral_ID__c != null)

You need to set that field on contact in your test.

Contact c = new Contact(/*existing data*/, Referral_Id__c='<something_here>');

As noted well by @Eric in the comments, you are simply smoke testing based on the code included in your question. Take a look at How to Write Good Unit Tests (emphasis mine):

Verify the results are correct

Verifying that your code works as you expect it to work is the most important part of unit testing. It’s also one of the things that Force.com developers commonly neglect. Unit tests that do not verify the results of the code aren’t true unit tests. They are commonly referred to as smoke tests, which aren’t nearly as effective or informative as true unit tests.

A good way to tell if unit tests are properly verifying results is to look for liberal use of the System.assert() methods. If there aren’t any System.assert() method calls, then the tests aren’t verifying results properly. And, no, System.assert(true); doesn’t count.

4
  • 2
    @Matt and the reason you did not. Arch that you were not entering the else- because you have no assert statements and are just covering lines – Eric Sep 21 '16 at 23:38
  • 2
    Argh, dang mobile - That was supposed to be "And the reason you did not catch" - And to clarify, if you had queried for the Opportunity records and asserted they were actually updated you would have seen that part of your code did not work properly. – Eric Sep 22 '16 at 2:14
  • 1
    I figured as much. Seems to bite us all now and then. – Adrian Larson Sep 22 '16 at 2:15
  • @Eric -- thanks for pointing that out, I'll definitely start using asserts as I get more comfortable with apex. – Matt Sep 23 '16 at 1:16

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.