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
Rohith Kumar 98Rohith Kumar 98 

Test class on trigger handler fail, why?

There is a custom object named Payment Order with Account as the lookup field.
I created a trigger on this custom object so that when creating a new record and choosing any Account as lookup then the values from the Account are populated in some fields of the Payment Order custom object.

For the above trigger, I created a Test Class:
@isTest
public class TestPaymentOrderTriggerHandler {
    @isTest static void TestUpdatePaymentContactInformationCheck(){
        //Creating and Inserting new Account
        Account newAccount = new Account(Name = 'Test Account', Phone = '1234567890', BillingStreet = 'Test Billing Street',
                                      ShippingStreet = 'Test Shipping Street', ShippingPostalCode = '625014', ShippingState = 'Test State',
                                     ShippingCountry = 'Test country');
        insert newAccount;
        
        //Creating  and Inserting new Payment Order with created account as lookup
        Payment_Order__c paymentOrder = new Payment_Order__c(Account__c = newAccount.Id);
        insert paymentOrder;
        
        Test.startTest();
        System.assertEquals(newAccount.Name, paymentOrder.Payee_Name__c);
        Test.stopTest();
    }
}

but the actual value in System.assertEquals is null however expected value is the name of the account which is correct.

I tried the same code in execute anoynamous window, the created Account name and Payment Order's payee name is the same and not null like in the test method.

Trigger working fine, then why I am getting null value in the test class in System.assertEquals(newAccount.Name, paymentOrder.Payee_Name__c); for the highlited parameter.
Maharajan CMaharajan C
Hi Rohith,

You need to query the paymentOrder to check the Payee_Name__c in Assert.
 
@isTest
public class TestPaymentOrderTriggerHandler {
    @isTest static void TestUpdatePaymentContactInformationCheck(){
        //Creating and Inserting new Account
        Account newAccount = new Account(Name = 'Test Account', Phone = '1234567890', BillingStreet = 'Test Billing Street',
                                      ShippingStreet = 'Test Shipping Street', ShippingPostalCode = '625014', ShippingState = 'Test State',
                                     ShippingCountry = 'Test country');
        insert newAccount;
        
        //Creating  and Inserting new Payment Order with created account as lookup
        Payment_Order__c paymentOrder = new Payment_Order__c(Account__c = newAccount.Id);
        insert paymentOrder;
		
		Payment_Order__c po = [Select Id, Payee_Name__c, Account__c from  Payment_Order__c where Id =: paymentOrder.id];
        system.debug( ' Payee_Name__c --> ' + po.Payee_Name__c) 
		
        Test.startTest();
        System.assertEquals(newAccount.Name, po.Payee_Name__c);
        Test.stopTest();
    }
}

Thanks,
Maharajan.C