0

I am trying to create a new case with record type to cover the lines in my apex class but throwing the inbound error. please help me to cover the lines enter image description here This is the full method

public void onAfterInsert()
    {
        // don't do anything if not licensed
        if (!FMA_isEnabled)
        {
            return;
        }

        // reset class variables
        carePlanList = new List<Case>();
        carePlanIds  = new List<Id>();
        dmlOpts      = new Database.DMLOptions();

        // map referral Case to careplan Case
        Map<Id,Id> refToPlanMap = new Map<Id, Id>();

        for (Case caseObj : newLst)
        {
            // if record type is CarePlan
            if (
                rtIdMap.containsKey(caseObj.RecordTypeId)
                &&
                rtIdMap.get(caseObj.RecordTypeId).getDeveloperName() == 'CarePlan'
                &&
                Util.getRecordTypeNamespace(caseObj.RecordTypeId) == 'HealthCloudGA'
                &&
                caseObj.Associated_Referral__c != null
            )
            {
                handleNewCarePlanAssignment(caseObj);

                refToPlanMap.put(caseObj.Associated_Referral__c, caseObj.Id);
            }
        }

        // set in handleNewCarePlanAssignment
        if (!carePlanIds.isEmpty() &&
            Schema.sObjectType.Case.fields.Assign_using_active_assignment_rule__c.isUpdateable()
            &&
            Schema.sObjectType.Case.fields.ParentId.isUpdateable())
        {
            for (Case plan : [SELECT Id, Assign_using_active_assignment_rule__c FROM CASE WHERE Id IN :carePlanIds])
            {
                plan.Assign_using_active_assignment_rule__c = false;
                plan.ParentId = null;
                plan.setOptions(dmlOpts);
                carePlanList.add(plan);
            }
        }

        // update care plans
        if(!carePlanList.isEmpty()){
            update carePlanList;
        }

        if (!refToPlanMap.keySet().isEmpty() && Schema.sObjectType.Case.fields.ParentId.isUpdateable()
            &&
            Schema.sObjectType.Case.fields.ConvertedToCarePlan__c.isUpdateable())
        {
            // get referral cases to update
            for (Case refCase : [SELECT Id, ConvertedToCarePlan__c, ParentId FROM Case WHERE Id IN :refToPlanMap.keySet()])
            {
                // set parent Careplan Case
                refCase.ParentId = refToPlanMap.get(refCase.Id);
                refCase.ConvertedToCarePlan__c = true;

                // add to list of updates
                referralList.add(refCase);
            }
        }

        // update referrals
        if(!referralList.isEmpty()){
            update referralList;
        }
    }

Test Class method

@testSetup static void setup() { UserTest.initTestUsers();

    TestUtilities.IS_FMA_ENABLED = true;

    System.runAs(UserTest.getTestUser(UserTest.UserType.mcm))
    {
        // TODO :: change to SOQL & filter by namespace?
        Map<String, Schema.RecordTypeInfo> caseRTMap = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName();

        // Account
        Account testPatient = TestUtilities.createPatientAccount();

        // Program
        HealthCloudGA__Program__c testProgram = (HealthCloudGA__Program__c) COMM_DataFactory.createSObject(new HealthCloudGA__Program__c(
                Name = 'Unit Test Program',
                HealthCloudGA__StartDate__c = Date.today(),
                HealthCloudGA__EndDate__c = Date.today() + 1
        ), true);

        Case referralCase = (Case) COMM_DataFactory.createSObject(
            new Case
            (
                RecordTypeId = caseRTMap.get('Referral').getRecordTypeId(),
                Program_Name__c = testProgram.Id
            ),
            true
        );

Case carePlanCase = (Case) COMM_DataFactory.createSObject( new Case ( RecordTypeId = caseRTMap.get('carePlan').getRecordTypeId(), Program_Name__c = testProgram.Id ), true ); } }

 @isTest
    static void testOnAfterInsert()
    {
        TestUtilities.IS_FMA_ENABLED = true;

        System.runAs(UserTest.getTestUser(UserTest.UserType.mcm))
        {
            Test.startTest();

            Map<String, Schema.RecordTypeInfo> caseRTMap = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName();

            List<Case> referralCase = [
                    SELECT Id, Program_Name__c, ContactId
                    FROM Case
                    WHERE RecordTypeId = :caseRTMap.get('Referral').getRecordTypeId()
            ];

             List<Case> carePlanCase = [
                    SELECT Id, Program_Name__c, ContactId
                    FROM Case
                    WHERE RecordTypeId = :caseRTMap.get('CarePlan').getRecordTypeId()
            ];



            Case carePlan = new Case();
            carePlan.RecordTypeId = caseRTMap.get('CarePlan').getRecordTypeId();
            carePlan.Program_Name__c = referralCase[0].Program_Name__c;
            carePlan.Associated_Referral__c = referralCase[0].Id;
            insert carePlan;
           Case Referral = new Case();
            Referral.RecordTypeId = caseRTMap.get('CarePlan').getRecordTypeId();
           Referral.Program_Name__c = carePlanCase[0].Program_Name__c;
          Referral.Associated_Referral__c = carePlanCase[0].Id;
            system.debug('referral'+ carePlanCase);
            insert Referral;


if(!carePlanCase.isEmpty()){
 carePlan.ParentId = referralCase[0].ParentId;
}

            referralCase = [
                    SELECT Id, ConvertedToCarePlan__c
                    FROM Case
                    WHERE RecordTypeId = :caseRTMap.get('Referral').getRecordTypeId()
            ];

            System.assert(referralCase[0].ConvertedToCarePlan__c == true, 'Referral Case - Converted to Care Plan not updated.');

            Test.stopTest();
        }
    }
0

1 Answer 1

1

Worrying about code coverage is premature when your unit tests are not working, which is the case here. This line:

Referral.Program_Name__c = carePlanCase[0].Program_Name__c;

throws an exception on accessing element 0 because that element does not exist - the list is empty.

So the next thing you do is look where the list came from:

         List<Case> carePlanCase = [
                SELECT Id, Program_Name__c, ContactId
                FROM Case
                WHERE RecordTypeId = :caseRTMap.get('CarePlan').getRecordTypeId()
        ];

and then you ask "Why is this query not returning any records?"

Now that can come from a variety of reasons - problems with criteria and so on - but here it's because you haven't created any data that matches the query. Reminder: unit tests can only access the records that they themselves create. You must create data that will match each query you expect to have a non-empty result.

I highly recommend reading all of the questions about unit tests under canonical-qa to learn how to write unit tests well. They are your chief guard against losing the trust of your users by shipping poor quality software.

8
  • I do not have problem with carePlan.Program_Name__c = referralCase[0].Program_Name__c; I have a problem with ``` Referral.Program_Name__c = carePlanCase[0].Program_Name__c;``` and I queried the Program_Name__c in my list
    – Teja
    Commented Apr 10, 2020 at 0:44
  • Is there an @testSetup method you have not shown us? Where is this data being created? The answer ultimately is the same - you have to make sure you have test data that matches your queries.
    – David Reed
    Commented Apr 10, 2020 at 0:46
  • Yes @testSetup is there. I've added in my test class above. can you take a look on it.
    – Teja
    Commented Apr 10, 2020 at 0:55
  • I edited my answer to reflect the data created in testSetup, but ultimately it's exactly the same answer. You need to create the right data to match all of your queries.
    – David Reed
    Commented Apr 10, 2020 at 0:56
  • create the right data means do I need to create a case data again in setup ?
    – Teja
    Commented Apr 10, 2020 at 0:59

Not the answer you're looking for? Browse other questions tagged .