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
golugolu 

Writing a test class for post callout?

Hi,
i am getting a post call out from external stsyem. i am creating or updating a  custom object record out of it. Can anyone please let em know how to write a test classs for it?
Here is my code:

global without sharing class ContactService {
    
    @HttpPost
    global static String insertContact(ContactInfoParser contactRec){
        //creation or update of application record. Mapping the fields from ContactInfoParser class.
        String RecordTypeApplicationId = Schema.SObjectType.Application__c.getRecordTypeInfosByName().get('Test').getRecordTypeId();        
        Application__c applicationObj = new Application__c();
        applicationObj.Id = contactRec.application.applicationId;
        applicationObj.applicant__c = contactRec.contact.contactId;
        applicationObj.RecordTypeId = RecordTypeApplicationId;
        applicationObj.assessPriorLearning__c = contactRec.application.assessPriorLearning;
        applicationObj.Status__c = contactRec.application.status;
        applicationObj.reason__c= contactRec.application.reason;
        applicationObj.priorLearningNotes__c= contactRec.application.priorLearningNotes;
        applicationObj.applingForCredit__c= contactRec.application.applingForCredit;
        upsert applicationObj;


Thanks
Best Answer chosen by golu
Glyn Anderson 3Glyn Anderson 3
I don't know anything about your ContactInfoParser object, so I can't write that, but something like:

<pre>
private static testMethod void testInsertContact()
{
    ContactInfoParser parser = new ContactInfoParser();    //  initialize this as appropriate
    Test.startTest();
    String result = ContactService.insertContact( parser );
    Test.stopTest();
    System.assertEquals( 'what the result should be', result, 'The result is wrong.' );
    List<Application__c> applications = [SELECT Id FROM Application__c];
    System.assertEquals( 1, applications.size(), 'Wrong number of applications created.' );
    //  assert that other things about the application is correct
}
</pre>
 

All Answers

Glyn Anderson 3Glyn Anderson 3
In your test method, create a ContactInfoParser object and call ContactService.insertContact with that object.  You can query to confirm that the Application__c record was created correctly, and you should assert that you get the expected String returned from the method.

You don't actually simulate the HTTPRequest coming into the API in your test class.  It's not like simulating an HTTP callout with a mock.
golugolu
Hi Glyn,

Can you please  give a sample code for reference?

Thanks.
Glyn Anderson 3Glyn Anderson 3
I don't know anything about your ContactInfoParser object, so I can't write that, but something like:

<pre>
private static testMethod void testInsertContact()
{
    ContactInfoParser parser = new ContactInfoParser();    //  initialize this as appropriate
    Test.startTest();
    String result = ContactService.insertContact( parser );
    Test.stopTest();
    System.assertEquals( 'what the result should be', result, 'The result is wrong.' );
    List<Application__c> applications = [SELECT Id FROM Application__c];
    System.assertEquals( 1, applications.size(), 'Wrong number of applications created.' );
    //  assert that other things about the application is correct
}
</pre>
 
This was selected as the best answer