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
Sammy ShkSammy Shk 

Test class for HTTP post Rest

Hello,

I'm new in Apex, I'm finding it difficult to write test class for Http Post, I'm sending some values from Salesforce to external system however I'm not sure how to write test class for this scenario. I did created test records but don't  know how to add Http post Test class.

Can someone help me how to add Http post test class in the test class which i created?
 
public class RadarUpdate {
    @future (callout=true)
	public static void postcallout(string Id) { 
	Patient_Satisfaction__c c = [select id, Name, Reporter_Phone__c,Reporter_First_Name__c,Reporter_Last_Name__c, Reporter_Email__c,
    Description_of_Feedback__c from Patient_Satisfaction__c where Patient_Relation__c ='Referred to Privacy Office' order by lastmodifiedDate desc limit 1];
    JSONGenerator gen = JSON.createGenerator(true);
	gen.writeStartObject();
	gen.writeObjectField('name', c.Name);
	gen.writeObjectField('incident_group_id', 7387);
       gen.writeObjectField('description',c.Description_of_Feedback__c);
	gen.writeFieldName('submitted_by');
	gen.writeStartObject();
	gen.writeStringField('given_name',c.Reporter_First_Name__c);
	gen.writeStringField('surname', c.Reporter_Last_Name__c);
	gen.writeStringField('phone',c.Reporter_Phone__c);
        gen.writeStringField('email',c.Reporter_Email__c);
	gen.writeEndObject();
	String jsonS = gen.getAsString(); 
	System.debug('jsonMaterials'+jsonS);
        Http http = new Http();
	HttpRequest request = new HttpRequest();
	request.setEndpoint('https://api.radarfirst.com/incidents');
	request.setMethod('POST');
	request.setHeader('Content-Type','application/json;charset=UTF-8');
	request.setHeader('User-agent', 'Salesforce-integration-client');
	request.setHeader('Authorization','Bearer 123');
         request.setBody(jsonS);
	// Set the body as a JSON object
	HttpResponse response = http.send(request);
	if (response.getStatusCode() != 201) {
    System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
	} else {
    System.debug(response.getBody());
	}
    }
}
 
@isTest
private class RadarUpdateTest {
    @isTest  public static void CreatePR(){
        
        rkl__RK_Hierarchy_Node__c Hr = new rkl__RK_Hierarchy_Node__c();
        Hr.Name= 'Hieararchy';
        Hr.rkl__Node_Level__c = 1.0;
        Hr.rkl__Node_Name__c = 'Node Name';
        insert Hr;
        
        Account ac = new Account();
        ac.Name= 'Test';
        insert ac;
        
        Contact con= new Contact();
        con.AccountId= ac.Id;
        con.FirstName ='First Name';
        con.LastName = 'Test Contact';
        con.Phone= '7896541233';
        con.Email= 'Test@123.com';
        con.RUSH_Email__c= '';
        insert con;       	
		
        Patient_Satisfaction__c Pr = new Patient_Satisfaction__c();
        Pr.Primary_Campus__c = Hr.Id;
        Pr.Primary_Facility__c= Hr.Id;
        Pr.Primary_Location__c= Hr.Id;
        Pr.Reporter__c=con.Id;
        Pr.Reporter_Phone__c=con.Id;
        Pr.Reporter_Email__c=con.Id;
        Pr.Description_of_Feedback__c= 'Testing Privacy office';
        Pr.Patient_Relation__c = 'Referred to Privacy Office';
        
        insert Pr;
        update Pr;
            
        
    }

}

 
ShirishaShirisha (Salesforce Developers) 
Hi Sammy,

Greetings!

Please find the sample test class code to cover the http request in the below document:

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

https://salesforce.stackexchange.com/questions/185163/a-testclass-for-restresource-httppost

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Sammy ShkSammy Shk
Thank you, I'm still struggling to write the test class
@istest
public class MyClassHTTPPost{

 static testMethod void  postcalloutTest(){
        
        Account ac = new Account();
        ac.Name= 'Test';
        insert ac;
        
        Contact con= new Contact();
        con.AccountId= ac.Id;
        con.FirstName ='First Name';
        con.LastName = 'Test Contact';
        con.Phone= '7896541233';
        con.Email= 'Test@123.com';
        con.RUSH_Email__c= 'Test@123.com';
        insert con;
         		
        Patient_Satisfaction__c Pr = new Patient_Satisfaction__c();
        Pr.Primary_Campus__c = 'a1pP0000001lfaKIAQ';
        Pr.Primary_Facility__c= 'a1pP00000022FLRIA2';
        Pr.Primary_Location__c= 'a1pP00000022FLcIAM';
        Pr.Reporter__c=con.Id;
        Pr.Reporter_Phone__c=con.Id;
        Pr.Reporter_Email__c= 'Test@123.com';
        Pr.Description_of_Feedback__c= 'Testing Privacy office';
        Pr.Patient_Relation__c = 'Referred to Privacy Office';
        
        update Pr;
  	 RadarUpdate reqst=new RadarUpdate();
   	String JsonMsg=JSON.serialize(reqst);
   	Test.startTest();

   RestRequest req = new RestRequest(); 
   RestResponse res = new RestResponse();

    req.requestURI = '/services/apexrest/DemoUrl';  //Request URL
    req.httpMethod = 'POST';//HTTP Request Type
    req.requestBody = Blob.valueof(JsonMsg);
    RestContext.request = req;
    RestContext.response= res;
    RadarUpdate.postcallout(Pr.Id);
    update Pr;
    Test.stopTest();

   }
}
It says MISSING_ARGUMENT, Id not specified in an update call: