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
Ganta SureshGanta Suresh 

Please help me to get class success and code coverage 85% -- actually test class getting failed. make change and revert me

ERROR:- System.NullPointerException: Attempt to de-reference a null object System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Account__c]: [Account__c]

Testclass:-

@isTest
public class FacilityTriggerTest {

    @isTest
    public static void updateagreementTest() {
        Apttus__APTS_Agreement__c agreement = new Apttus__APTS_Agreement__c();
        agreement.Name = 'Test Agreement';
        insert agreement;

        Facility__c facility = new Facility__c();
        //facility.Name = 'Test Facility';
        facility.Agreement__c = agreement.Id;
        //facility.Davita_legal_entity__c = 'Test Legal Entity';
        //facility.Facility_Division__c = 'Test Division';
        //facility.Facility_Group__c = 'Test Group';
        //.Davita_legal_Entity_Name__c = 'Test Legal Entity Name';
        //facility.Facility_Name__c = 'Test Facility Name';
        //facility.Facility_Number__c = '123456';
        insert facility;

        List<Facility__c> facilityList = new List<Facility__c>();
        facilityList.add(facility);

        Test.startTest();
        FacilityHandler.updateagreement(facilityList);
        Test.stopTest();

        Apttus__APTS_Agreement__c updatedAgreement = [SELECT Id, Facility_Name_for_Template__c, Facility_Number_for_Template__c, Division_Name__c, Palmer_Group__c, Davita_Legal_Entity_2__c
                                                      FROM Apttus__APTS_Agreement__c
                                                      WHERE Id = :agreement.Id];

        System.assertEquals(facility.Davita_legal_entity__c, updatedAgreement.Davita_Legal_Entity_2__c);
        //Changed as Divisin__c to Division_Name__c on 28/06/2023
        System.assertEquals(facility.Facility_Division__c, updatedAgreement.Division_Name__c);
        System.assertEquals(facility.Facility_Group__c, updatedAgreement.Palmer_Group__c);
        System.assertEquals(facility.Davita_legal_Entity_Name__c, updatedAgreement.Davita_Legal_Entity_2__c);
        System.assertEquals(facility.Facility_Name__c, updatedAgreement.Facility_Name_for_Template__c);
        System.assertEquals(facility.Facility_Number__c, updatedAgreement.Facility_Number_for_Template__c);
    }

    @isTest
    public static void facilityMethodTest() {
        Apttus__APTS_Agreement__c agreement = new Apttus__APTS_Agreement__c();
        agreement.Name = 'Test Agreement';
        insert agreement;

        Facility__c facility = new Facility__c();
        //facility.Name = 'Test Facility';
        facility.Agreement__c = agreement.Id;
        facility.Account__r.Name = 'Test Account';
        insert facility;

        List<Facility__c> newFacilityList = new List<Facility__c>();
        newFacilityList.add(facility);

        List<Facility__c> oldFacilityList = new List<Facility__c>();

        Test.startTest();
        FacilityHandler.facilityMethod(newFacilityList, oldFacilityList, true, false);
        Test.stopTest();

        Apttus__APTS_Agreement__c updatedAgreement = [SELECT Id, Facility_Name__c
                                                      FROM Apttus__APTS_Agreement__c
                                                      WHERE Id = :agreement.Id];

        System.assertEquals(facility.Account__r.Name, updatedAgreement.Facility_Name__c);
    }
}

APEXclass:-
trigger FacilityTrigger on Facility__c(after insert, after update, after delete) {

    if (trigger.isAfter && (trigger.IsInsert || trigger.IsUpdate)) {
        FacilityHandler.updateagreement(trigger.New);
    }
    if (trigger.isAfter && (trigger.IsInsert || trigger.IsUpdate || trigger.IsDelete)) {
        FacilityHandler.facilityMethod(trigger.new, trigger.old, trigger.IsInsert, trigger.IsUpdate);
    }
}
Ganta SureshGanta Suresh
STACK TRACE:-
Class.FacilityTriggerTest.facilityMethodTest: line 50, column 1
Class.FacilityTriggerTest.updateagreementTest: line 19, column 1
SwethaSwetha (Salesforce Developers) 
HI Suresh,
Based on the error message, it looks like you need to set the Account__c field in the test class before inserting the Facility__c record.
 
@isTest
public class FacilityTriggerTest {

    @isTest
    public static void updateagreementTest() {
        Apttus__APTS_Agreement__c agreement = new Apttus__APTS_Agreement__c();
        agreement.Name = 'Test Agreement';
        insert agreement;

        Facility__c facility = new Facility__c();
        facility.Agreement__c = agreement.Id;
        facility.Account__c = 'Account Id'; // Replace 'Account Id' with the actual Account record Id
        insert facility;

        List<Facility__c> facilityList = new List<Facility__c>();
        facilityList.add(facility);

        Test.startTest();
        FacilityHandler.updateagreement(facilityList);
        Test.stopTest();

        // Rest of the test assertions
    }

    @isTest
    public static void facilityMethodTest() {
        Apttus__APTS_Agreement__c agreement = new Apttus__APTS_Agreement__c();
        agreement.Name = 'Test Agreement';
        insert agreement;

        Facility__c facility = new Facility__c();
        facility.Agreement__c = agreement.Id;
        facility.Account__c = 'Account Id'; // Replace 'Account Id' with the actual Account record Id
        insert facility;

        // Rest of the test method
    }
}
Related: https://salesforce.stackexchange.com/questions/358315/apex-test-class-throwing-nullpointerexception

https://developer.salesforce.com/forums/?id=9060G000000Bhq7QAC

If this information helps, please mark the answer as best. Thank you