1

I've written a test code for a class which I thought would be just about 100% coverage - but it's still showing only 0% in the class after the test runs. The class itself is running correctly.

Here is the class :

public class WeeklyMailerOpp_A implements Schedulable
{
    public void execute (SchedulableContext scx)
    {
        List<Wealth__c> recordsA = [
            SELECT No_IXI_Relationship__c, Relationship_HHAge__c, Name, OwnerId, Distributed__c,Solicitable__c,Code_Targeted__c,Division_Letter__c FROM Wealth__c
            WHERE Distributed__c = false AND Solicitable__c = True AND Code_Targeted__c = 'Yes' AND Relationship_HHAge__c >= 25 AND No_IXI_Relationship__c != True AND Division_Letter__c = 'A' LIMIT 25   
        ];

        List<Wealth__c> recordsB = [
            SELECT No_IXI_Relationship__c, Relationship_HHAge__c, Name, OwnerId, Distributed__c,Solicitable__c,Code_Targeted__c,Division_Letter__c FROM Wealth__c
            WHERE Distributed__c = false AND Solicitable__c = True AND Code_Targeted__c = 'Yes' AND Relationship_HHAge__c >= 25 AND No_IXI_Relationship__c != True AND Division_Letter__c = 'B' LIMIT 25   
        ];

       for(Wealth__c recordA : recordsA)
        {
            recordA.Distributed__c = true;  
            recordA.Distribution_Date__c = date.TODAY();         
        }

       for(Wealth__c recordB : recordsB)
        {
            recordB.Distributed__c = true;  
            recordB.Distribution_Date__c = date.TODAY();         
        }

        update recordsA;
        update recordsB;

    }
}

...and here is the test code :

    @isTest
    private class TestWeeklyMailerOpp_A
    {    
        static testMethod void UnitTestWeeklyMailerOpp_A()
        {     

**String cronExpr = '0 0 0 15 3 ? 2022';
    Test.StartTest();
    System.schedule('myJobTestJobName', cronExpr, new WeeklyMailerOpp_A());**   

            Wealth__c mailerA = new Wealth__c();
            mailerA.Distributed__c = False; 
            mailerA.Account__c = '001i000000dmNHI'; 
            mailerA.Code__c = '33';  
            mailerA.Division__c = 'aCX31000000PAuE';      

            Wealth__c mailerB = new Wealth__c();
            mailerB.Distributed__c = False;  
            mailerB.Account__c = '001i000000XmrUI';
            mailerB.Code__c = '33';  
            mailerB.Division__c = 'aCX31000000PAuJ'; 

            insert mailerA;
            insert mailerB;

            List<Wealth__c> recordsA = [
                SELECT Name, OwnerId, Solicitable__c,Distributed__c,Code_Targeted__c,Division_Letter__c FROM Wealth__c
                WHERE Distributed__c = false AND Solicitable__c = True AND Code_Targeted__c = 'Yes' AND Division_Letter__c = 'A' LIMIT 10   
            ];    

            for(Wealth__c recordA : recordsA)
            {
                recordA.Distributed__c = true;  
                recordA.Distribution_Date__c = date.TODAY();         
            }

             List<Wealth__c> recordsB = [
                SELECT Name, OwnerId, Distributed__c,Code_Targeted__c,Division_Letter__c FROM Wealth__c
                WHERE Distributed__c = false AND Solicitable__c = True AND Code_Targeted__c = 'Yes' AND Division_Letter__c = 'B' LIMIT 10   
            ];    

            for(Wealth__c recordB : recordsB)
            {
                recordB.Distributed__c = true;  
                recordB.Distribution_Date__c = date.TODAY();         
            }

            update recordsA;
            update recordsB;  

 **Test.stopTest();**                   
        }    
    }

I must be doing something seriously wrong but I can't figure out what. Any ideas ?

2
  • 4
    Well, you're not calling your class in your test method. Jul 28, 2017 at 16:09
  • @MartinLezer - I made changes. That's now at 49%. Can you tell me what I'm not covering ?
    – Zoom_v
    Jul 28, 2017 at 18:47

1 Answer 1

7

You never actually run the job. You need to either run it by calling system.schedule or calling the execute method directly.

3
  • Yha, I suppose that would help, wouldn't it ? I just added some lines to the test code. I am now getting 49% coverage. While 49% is much better than 0% it's not quite what I wanted. Can you see what I am not covering ? The lines I added are within the asterisk. Thank you.
    – Zoom_v
    Jul 28, 2017 at 18:46
  • 2
    @Zoom_v That's kind of a completely different question! Also your edits to the question didn't really make clear what's not covered, but the only lines which wouldn't get covered are those inside the for loops. In other words, your queries didn't return data.
    – Adrian Larson
    Jul 28, 2017 at 20:04
  • You're right - different question. Thanks @Adrian.
    – Zoom_v
    Jul 28, 2017 at 20:58

You must log in to answer this question.

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