I have a @HttpPost method in @RestResource class:

 global with sharing class HttpClass {

global static String updateCustomObject() {
    //Fetching request

    RestRequest  req     = RestContext.request;   
    //Getting JSON String from request body
    String jsonInput  = req.requestBody.toString();
    JSONParser parser = JSON.createParser(jsonInput);

    System.debug('Parser:' + jsonInput);

    jsonInput = jsonInput.replace(' ','');

    Object jsonUnpacked =  json.deserializeUntyped(jsonInput);

    User gUser = (User)JSON.deserialize(jsonInput, User.class);

    system.debug('user ' + gUser);

    X2017_questionnaire__c quest = new X2017_questionnaire__c(Name = gUser.name,Email_address__c = gUser.email,Pain_Factor_1__c = gUser.pain1,
                                                              Pain_Factor_2__c = gUser.pain2,Pain_Factor_3__c = gUser.pain3,
                                                              Pain_Factor_4__c = gUser.pain4,Pain_Factor_5__c = gUser.pain5,
                                                              Project_Objective_1__c = gUser.obj1,Project_Objective_2__c = gUser.obj2,
                                                              Project_Objective_3__c = gUser.obj3,Project_Objective_4__c = gUser.obj4,
                                                              Project_Objective_5__c = gUser.obj5);

    insert quest;

    /* //Below JSON Generator create response string
    JSONGenerator jsonGenerator = JSON.createGenerator(true);

    jsonGenerator.writeStringField('Status', 'Success/Fail');

    String responseJSONString = jsonGenerator.getAsString();*/

    List<X2017_questionnaire__c> lstOfQuest = [SELECT name,Email_address__c FROM X2017_questionnaire__c WHERE Email_address__c =:gUser.email];
    if (!lstOfQuest.isEmpty()){
        return 'Created Succesfully';
    }else {
        return 'Error';


public class User {
    public String name;
    public String email;
    public String pain1;
    public String pain2;
    public String pain3;
    public String pain4;
    public String pain5;
    public String obj1;
    public String obj2;
    public String obj3;
    public String obj4;
    public String obj5;



How do I write a test class for the HTTPPost method? I tried using HttpMockCallout, but it works with HttpResponse and for my @HttpPost method I need a HttpRequest test.


You can do it in the following way, very similar to common apex class testing

static void testHttpPost() {
    // prepare test-data

    //As Per Best Practice it is important to instantiate the Rest Context
    RestRequest req = new RestRequest();
    RestResponse res = new RestResponse();
    req.requestURI = '/services/apexrest/Something'; //Request URL
    req.httpMethod = 'POST';

    RestContext.request = req;
    RestContext.response= res;

        String actual = null;//HttpClass.updateCustomObject();

    System.assertEquals('expected value', actual, 'Value is incorrect');
  • But do you actually send a request? I don't think you can Http h = Http(); h.send(req); I don't quite get that part – Alan Abishev Jul 20 '17 at 12:23
  • no, you cant do request in this way. But you can test functionality, as I answered – Oleksandr Berehovskyi Jul 20 '17 at 12:25
  • Sorry, I was getting the null error because I didn't set the JSON body. Thanks for the help. Spasibo! – Alan Abishev Jul 20 '17 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.