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
Sheldon Callahan 16Sheldon Callahan 16 

If my dml insert happeneds in a test and that information is not committed to the DB how can my assertions work?

I have this test code to test if an account is enabled.
@isTest
private class PIContractsCronTest {
    @isTest static void PIContractsCronTest() {

        //Scenario: 
        ///Given a nightly build at 11:59
        //Arrange

        Account acct = new Account();
        acct.Name = 'Test Account - Sheldon 3';
        acct.recordTypeId = '01250000000DfwkAAC';
        acct.Primary_Sector__c = 'General Inclusion';
        acct.Name_Code__c = 'TSTSHEL';
        insert acct;

        Account parentAcct = [SELECT Id,Name FROM Account WHERE Name = 'Test Account - Sheldon 3'];
        PI_Contract__c pic = new PI_Contract__c(Name ='Test Contract', Account__c = parentAcct.Id, Local_Currency_Peg_Enabled__c = 'No', Contract_Start_Date__c = DATE.TODAY());

        
        insert pic;

        // When an Account object has a PI CONTRACT with the “Status = current” AND current date is greater than or equal to “Contract Start Date” 
        //Act
        Test.StartTest();
        PIContractsCron cron = new PIContractsCron();
        Account checkPI = [SELECT Id,Name, PI_Enabled__c FROM Account WHERE Id = :parentAcct.Id];
      
        Test.StopTest();
        // Then set the field PI Contracts Enabled to true on the Account Object
        //Assert
          System.debug('Check PI:::'+checkPI);
   
        System.assertEquals(true,checkPI.PI_Enabled__c,'PI is not Enabled on this Account');

    }


}

This code runs this function

public class PIContractsCron {
    public PIContractsCron() {
       List<PI_Contract__c> contractList = [SELECT Id, Status__c, Contract_Start_Date__c, Account__c FROM PI_Contract__c WHERE Status__c = 'Current' AND Contract_Start_Date__c >= TODAY];
        Map <Id, Account> acctMap = new Map<Id, Account>();
        for(PI_Contract__c contract: contractList){
            Id acctNo = contract.Account__c;
            Account acct = [SELECT Id, PI_Enabled__c FROM Account WHERE Id = :acctNo];
            acctMap.put(acctNo,acct);
        }

		List <Account> enabledAcct = new List<Account>();
		for(Account a: acctMap.values()){
			a.PI_Enabled__c = true;
			enabledAcct.add(a);
		}

        System.debug('Enabled Accounts:::'+ enabledAcct);

        update enabledAcct;
    }
}
 

I can tell that the dml from the test is not showing up in this class because the  System.debug('Enabled Accounts:::'+ enabledAcct); comes back empty. What am I doing wrong?

Deepali KulshresthaDeepali Kulshrestha
Hi Sheldon,

The error is in your test class is have not filled the value of account field 'PI_Enabled__c ' during insertion thats the reason your assertion is not showing the proper result.
And always try to write assert statement inside Test.StartTest(); and Test.StopTest(); and one more thing, in your apex class you have written SOQL inside a for loop please try to written soql outside the for loop because it can hit the governor limit. I have highlighted the changes I have made in your test class with **.
@isTest 
private class PIContractsCronTest {
@isTest static void PIContractsCronTest() { 
//Scenario: 
///Given a nightly build at 11:59 
//Arrange 
Account acct = new Account(); 
acct.Name = 'Test Account - Sheldon 3';
acct.recordTypeId = '01250000000DfwkAAC';
acct.Primary_Sector__c = 'General Inclusion'; 
acct.Name_Code__c = 'TSTSHEL'; 
**acct.PI_Enabled__c  = true;
insert acct;
Account parentAcct = [SELECT Id,Name FROM Account WHERE Name = 'Test Account - Sheldon 3']; 
PI_Contract__c pic = new PI_Contract__c(Name ='Test Contract', Account__c = parentAcct.Id, Local_Currency_Peg_Enabled__c = 'No', Contract_Start_Date__c = DATE.TODAY());
insert pic; 
// When an Account object has a PI CONTRACT with the “Status = current” AND current date is greater than or equal to “Contract Start Date” 
//Act  
Test.StartTest(); 
PIContractsCron cron = new PIContractsCron(); 
Account checkPI = [SELECT Id,Name, PI_Enabled__c FROM Account WHERE Id = :parentAcct.Id]; 
**System.assertEquals(true,checkPI.PI_Enabled__c,'PI is not Enabled on this Account'); } 
Test.StopTest(); 
// Then set the field PI Contracts Enabled to true on the Account Object
//Assert 
System.debug('Check PI:::'+checkPI); 

}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Sheldon Callahan 16Sheldon Callahan 16
So the value should be added from the PIContractsCron cron = new PIContractsCron(). That is the reason for the test. I want to run this function on a cron job to switch the accounts with a PI_Contract with certain values to the Account.PI_Enabled = true.  When I create the account I don't want to set this yet.
Sheldon Callahan 16Sheldon Callahan 16
So the real problem is I can't tell if the test items inserted are being treated by my class PIContractsCron() as real records since the test to not really insert them. I know the code works with real records