+ Start a Discussion
Harsh P.Harsh P. 

How to Test System.today().day() in test class

How to Test System.today().day() in test class.
I have written test class for following Batch:

My Batch Is::

global class ASGPaymentFailedBatch implements Database.Batchable<sObject>,Database.stateful{
    
    global Database.QueryLocator start(Database.BatchableContext BC){ 
        String type ='Monthly';
        String status = 'Hold';
        String transStatus = 'failed';
        string query = 'SELECT id,Transaction_Status__c,Payment__r.Monthly_Parker__r.Email__c FROM Transaction__c WHERE Transaction_Status__c=:transStatus AND Payment__r.Monthly_Parker__r.Revenue_Type__c=:type AND Payment__r.Monthly_Parker__r.Status__c=:status ORDER BY CreatedDate DESC';  
        
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Transaction__c> scope){
        
        Set<String> friendlyEmailIdSet = new Set<String>();
        Set<String> suspendEmailIdSet = new Set<String>();
        Set<String> mParkerIdSet = new Set<String>();

        for(Transaction__c eachTransaction : scope){
            if(eachTransaction.CreatedDate.day()== 01 || eachTransaction.CreatedDate.day()== 1){
                if(System.today().day() == 02 || System.today().day() == 2 || System.today().day() == 03 || System.today().day() == 3){
                    friendlyEmailIdSet.add(eachTransaction.Payment__r.Monthly_Parker__r.Email__c);
                }
                else if(System.today().day() == 04 || System.today().day() == 4 ){
                    suspendEmailIdSet.add(eachTransaction.Payment__r.Monthly_Parker__r.Email__c);
                }
                mParkerIdSet.add(eachTransaction.Payment__r.Monthly_Parker__c);
            }
        }
        
        
        List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();

        String friendlyID = [select id from EmailTemplate where DeveloperName='Friendly_Notice'].id;
        String suspendID = [select id from EmailTemplate where DeveloperName='Suspended'].id;
        
                
        for(M_Parker__c mParker : [SELECT ID,Email__c FROM M_Parker__c WHERE ID IN : mParkerIdSet]){
            Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
            if(friendlyEmailIdSet.size()>0)
                msg.setTemplateId( friendlyID );
            else if(suspendEmailIdSet.size()>0){
                msg.setTemplateId( suspendID );
            }       
            msg.setTargetObjectId(mParker.id);
            msg.setToAddresses(new String[] {mParker.Email__c});
            lstMsgs.add(msg);           
        }   

        Messaging.sendEmail(lstMsgs);      
    }

    global void finish(Database.BatchableContext BC){
        
    }
}


My Test Class is::

@isTest
public class ASGPaymentFailedBatchTest {
    static testMethod void InsertPayibleTransaction(){
        
        Account accountObj = new Account();
        accountObj.Name = 'TestAccount';
        insert accountObj;
        
        Contact contactObj = new Contact();
        contactObj.LastName = 'Patil';
        contactObj.AccountId =accountObj.Id;
        contactObj.FirstName = 'Sachin';
        contactObj.Email ='sachin@gmail.com'; 
        contactObj.Phone = '9988767856';    
        insert contactObj;
        
        Location__c objLocation = new Location__c();
        objLocation.Name = 'WashingtonDC';
        objLocation.Loc__c = 0;
        objLocation.Location_Number__c = '22';
        objLocation.Address__c = '5454 Wisconsin Ave.';
        objLocation.City__c = 'Chevy Chase';
        objLocation.State__c = 'AK';
        insert objLocation;
        
        M_Parker__c objParker = new M_Parker__c();
        objParker.Account__c = contactObj.AccountId;
        objParker.Auto_color__c = 'Teal';
        objParker.Auto_Make__c = 'Audi';
        objParker.Auto_Model__c = 'audi 005';
        objParker.Auto_Year__c = '2018';
        objParker.Lic_Plate_State__c ='MH127788';
        objParker.Tenant__c = 'No';
        objParker.FacID__c = objLocation.Id;
        objParker.MonthlyParkerName__c = contactObj.Id;
        objParker.Revenue_Type__c = 'Monthly';
        objParker.Status__c = 'Hold';
        //objParker.Email__c = 'test@gmail.com';
        insert objParker;
         
        M_Parker__c pak = [Select Email__c from M_Parker__c where Id =: objParker.Id ];
        system.debug('pak-->'+pak);
        
        Payment__c ObjPayiblePayment = new Payment__c();
        //ObjPayiblePayment.Subscription_Product_Id__c = 'prod_EGzTyy8IEX2mAP';
        ObjPayiblePayment.Subscription_Customer_Id__c = 'cus_EGzTK9y1W2jYc6'; 
        //ObjPayiblePayment.Customer_Id__c = contactObj.Id;
        ObjPayiblePayment.Subscription_Id__c = 'sub_EGzT63XNdP1O6y';
        ObjPayiblePayment.Phone__c = '9968694934';  
        ObjPayiblePayment.Card_Type__c = 'visa';
        ObjPayiblePayment.Monthly_Parker__c = objParker.Id;
        ObjPayiblePayment.Customer_Name__c =  'Test Community';
        //ObjPayiblePayment.Payment_Gateway__c = ObjPaymentGateway.Id;    
        ObjPayiblePayment.Payment_Description__c = 'Test--28 - Barlow Building 2019-01-03 00:00:00';
        ObjPayiblePayment.Subscription_Name__c = 'TestSubscription';
        ObjPayiblePayment.Billing_ZIP_Postal_Code__c ='411028'; 
        ObjPayiblePayment.Billing_State_Province__c = 'Ny'; 
        ObjPayiblePayment.Billing_City__c ='TestCity'; 
        ObjPayiblePayment.Billing_Street__c = 'TestStreet';
        ObjPayiblePayment.Customer_Email__c = 'test@gmail.com';
        ObjPayiblePayment.Last_Name__c = 'TestThis';
        ObjPayiblePayment.First_Name__c = 'MyTest';
        ObjPayiblePayment.Amount__c = 100;
        ObjPayiblePayment.Exp_Year__c = '2026';
        ObjPayiblePayment.Exp_Month__c = '05';
        ObjPayiblePayment.Name_on_the_Card__c ='TestTest';
        ObjPayiblePayment.Billing_Country__c = 'US';
        ObjPayiblePayment.Currency_Type__c = 'USD';
        //ObjPayiblePayment.CVV__c = '233';
        ObjPayiblePayment.Card_Number__c = '1111222233334444';
        Insert ObjPayiblePayment;
        
        Transaction__c ObjTransaction = new Transaction__c();
        ObjTransaction.Transaction_Status__c = 'failed';
        ObjTransaction.Payment__c = ObjPayiblePayment.Id; 
        Insert ObjTransaction;
        
        DateTime fixDate = DateTime.newInstance(2012, 7, 01, 3, 3, 3);
        Test.setCreatedDate(ObjTransaction.Id, fixDate);

        
        system.debug('ObjTransaction-->'+ObjTransaction);
        
        Test.startTest();
        ASGPaymentFailedBatch objASGPaymentFailed = new ASGPaymentFailedBatch();
        DataBase.executeBatch(objASGPaymentFailed);
        Test.stopTest();
    }

}
 
Raj VakatiRaj Vakati
Yse Test.setCreatedDate(recordId, createdDatetime)
Sets CreatedDate for a test-context sObject.

 
Account account = new Account(Name = 'Test'); 
insert account;
 Datetime todaysDt= Datetime.now();
 Test.setCreatedDate(account.Id, todaysDt);