3

I have a @HttpPost method in @RestResource class:

@RestResource(urlMapping='/Something/*') global with sharing class HttpClass { @HttpPost 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); System.debug(jsonUnpacked); 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.writeStartArray(); jsonGenerator.writeStartObject(); //jsonGenerator.writeStringField('Email',emailFromJSONInput); jsonGenerator.writeStringField('Status', 'Success/Fail'); jsonGenerator.writeEndObject(); jsonGenerator.writeEndArray(); 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.

1 Answer 1

6

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

@isTest 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; Test.startTest(); String actual = null;//HttpClass.updateCustomObject(); Test.stopTest(); System.assertEquals('expected value', actual, 'Value is incorrect'); } 
3
  • 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 Commented Jul 20, 2017 at 12:23
  • no, you cant do request in this way. But you can test functionality, as I answered Commented Jul 20, 2017 at 12:25
  • Sorry, I was getting the null error because I didn't set the JSON body. Thanks for the help. Spasibo! Commented Jul 20, 2017 at 12:33

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.