function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AnbuAnbu 

Apex test class doesn’t perform update

Hi everyone,

I am new with Apex and and still have a lot to learn. I have built a schedulable class that should check some records and perform an update in the same object (quite simple). Problem is the test class that doesn't get enough code coverage. I have used the anonymous window and records are created. Problem is the UPDATE which is not performed.

here the class:



Apex Class

global class UpdateRollingAvgMonthlyRevenueJob implements Schedulable {
    global void execute(SchedulableContext ctx){
        //Query Acct Revenue Month that needs to be updated
    List<Acct_Revenue_Month__c> acctRevenueMonthList = [SELECT Id, Rolling_AvgMonRev__c, Rolling_AvgMonRevforFormula__c 
                                                            FROM Acct_Revenue_Month__c
                                                            WHERE IsAvgMonthRollCompare__c = TRUE ];
       
        List<Acct_Revenue_Month__c> acctRevenueMonthListToBeUpdated = new List<Acct_Revenue_Month__c>();
        
        if(!acctRevenueMonthList.isEmpty()){
            for(Acct_Revenue_Month__c arm : acctRevenueMonthList){
                Acct_Revenue_Month__c AcctRevMonth = new Acct_Revenue_Month__c ();
                AcctRevMonth.Rolling_AvgMonRevforFormula__c = arm.Rolling_AvgMonRev__c;
acctRevenueMonthListToBeUpdated.add(arm);                
            }
            update acctRevenueMonthListToBeUpdated;
        }
    }
}



Test Class


@isTest
private class UpdateRollingAvgMonthlyRevenueTest {
    public static String CRON_EXP = '0 0 0 15 3 ? 2022';
    static testmethod void testScheduledJob() {
        
         Date firstDayOfMonth = System.today().toStartOfMonth();
    
        //Create acct Revenue Month
        Acct_Revenue_Month__c arm = new Acct_Revenue_Month__c();
        arm.Name = 'TestAcct';
        arm.Rolling_AvgMonRevforFormula__c = 1; 
        
        insert arm;
        
        arm.Rolling_AvgMonRevforFormula__c = arm.Rolling_AvgMonRev__c;
        update arm;
        
        Test.startTest();
        // Schedule the test job
        String jobId = System.schedule('ScheduledApexTest',
           CRON_EXP,
            new UpdateRollingAvgMonthlyRevenueJob());
        // Stopping the test will run the job synchronously
        Test.stopTest();   
        
        arm = [SELECT Name,Tier__c,Company_Group_Monthly__c,Rolling_AvgMonRevforFormula__c,Rolling_AvgMonRev__c,LastModifiedDate
                                FROM Acct_Revenue_Month__c WHERE id = :arm.Id];
        
        system.assertNotEquals(null, arm.Rolling_AvgMonRevforFormula__c, 'This record has been modified');
        arm.Rolling_AvgMonRevforFormula__c = arm.Rolling_AvgMonRev__c;
        update arm;
    }
}

ankit bansalankit bansal
Hi Anbu,
Your code cant work since you are not passing the id of the record which you want to update to the "Acct_Revenue_Month__c AcctRevMonth = new Acct_Revenue_Month__c ();"
I have modified the code as below-
global class UpdateRollingAvgMonthlyRevenueJob implements Schedulable {
    global void execute(SchedulableContext ctx){
        //Query Acct Revenue Month that needs to be updated
    List<Acct_Revenue_Month__c> acctRevenueMonthList = [SELECT Id, Rolling_AvgMonRev__c, Rolling_AvgMonRevforFormula__c 
                                                            FROM Acct_Revenue_Month__c
                                                            WHERE IsAvgMonthRollCompare__c = TRUE ];
        
        if(!acctRevenueMonthList.isEmpty()){
            for(Acct_Revenue_Month__c arm : acctRevenueMonthList){
                arm.Rolling_AvgMonRevforFormula__c = arm.Rolling_AvgMonRev__c;
            }
            update acctRevenueMonthList;
        }
    }
}


@isTest
private class UpdateRollingAvgMonthlyRevenueTest {
    public static String CRON_EXP = '0 0 0 15 3 ? 2022';
    static testmethod void testScheduledJob() {
        
        Date firstDayOfMonth = System.today().toStartOfMonth();
    
        //Create acct Revenue Month
        Acct_Revenue_Month__c arm = new Acct_Revenue_Month__c();
        arm.Name = 'TestAcct';
        arm.Rolling_AvgMonRev__c = 1; 
        arm.IsAvgMonthRollCompare__c = true;
        insert arm;
        
        Test.startTest();
        // Schedule the test job
        String jobId = System.schedule('ScheduledApexTest',
           CRON_EXP,
            new UpdateRollingAvgMonthlyRevenueJob());
        // Stopping the test will run the job synchronously
        Test.stopTest();   
        
        arm = [SELECT Name,Tier__c,Company_Group_Monthly__c,Rolling_AvgMonRevforFormula__c,Rolling_AvgMonRev__c,LastModifiedDate
                                FROM Acct_Revenue_Month__c WHERE id = :arm.Id];
        
        system.assertNotEquals(arm.Rolling_AvgMonRev__c, arm.Rolling_AvgMonRevforFormula__c, 'This record has not been modified');
    }
}

Also I have updated the test method to test the update you perform in the schedualble class.