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
Austin HensonAustin Henson 

New to Salesforce Development and need help writing a test class for this Apex Class

Hi all I am new to the Salesforce development side and am wondering how to create a test class for the below Apex Class I have written. I understand I need to create test data in the test class, but I am not sure how to test out the last part of this class which is calling another Apex Class from a managed package that exports Contact records to a 3rd platform. Any help and guidance would be much appreciated.
global class ExportBackgroundCheckCompleteToGolden {

    @InvocableMethod(label='Export Background Check Completed Volunteers To Golden' description='Exports contacts who have completed their background check with Sterling Volunteers to Golden' category='Contact')

    public static List<Contact> BackgroundCheckCompleteToGolden(List<Contact> contacts) {

        List<Contact> exported = new List<Contact>();

        if ((contacts == null) || (contacts.size() == 0)) {

            System.debug('No contacts');

            return exported;

        } else {

            System.debug('There are ' + contacts.size() + ' contacts.');

        }

        for (Contact contact: contacts) {

            if ((contact != null) && (contact.VVSA__VV_Status__c == 'Eligible')) {

                exported.add(contact);

            }

        }

        if (exported.size() > 0) {

            goldenapp.ExportToGoldenBatch.runSyncJob(exported, 'Background Check Completed');

        }

        return exported;

    }

}

 
Best Answer chosen by Austin Henson
Shri RajShri Raj
@isTest
private class TestExportBackgroundCheckCompleteToGolden {

    static testMethod void testBackgroundCheckCompleteToGolden() {

        // create a list of contacts
        List<Contact> contacts = new List<Contact>();
        Contact c1 = new Contact();
        c1.FirstName = 'John';
        c1.LastName = 'Doe';
        c1.VVSA__VV_Status__c = 'Eligible';
        contacts.add(c1);

        Contact c2 = new Contact();
        c2.FirstName = 'Jane';
        c2.LastName = 'Doe';
        c2.VVSA__VV_Status__c = 'Not Eligible';
        contacts.add(c2);

        // test the method
        List<Contact> exported = ExportBackgroundCheckCompleteToGolden.BackgroundCheckCompleteToGolden(contacts);

        // verify the output
        System.assertEquals(1, exported.size());
        System.assertEquals('John', exported[0].FirstName);
        System.assertEquals('Doe', exported[0].LastName);
    }
}

 

All Answers

Shri RajShri Raj
@isTest
private class TestExportBackgroundCheckCompleteToGolden {

    static testMethod void testBackgroundCheckCompleteToGolden() {

        // create a list of contacts
        List<Contact> contacts = new List<Contact>();
        Contact c1 = new Contact();
        c1.FirstName = 'John';
        c1.LastName = 'Doe';
        c1.VVSA__VV_Status__c = 'Eligible';
        contacts.add(c1);

        Contact c2 = new Contact();
        c2.FirstName = 'Jane';
        c2.LastName = 'Doe';
        c2.VVSA__VV_Status__c = 'Not Eligible';
        contacts.add(c2);

        // test the method
        List<Contact> exported = ExportBackgroundCheckCompleteToGolden.BackgroundCheckCompleteToGolden(contacts);

        // verify the output
        System.assertEquals(1, exported.size());
        System.assertEquals('John', exported[0].FirstName);
        System.assertEquals('Doe', exported[0].LastName);
    }
}

 
This was selected as the best answer
Austin HensonAustin Henson
Shri, thanks so much for providing this it worked perfectly!