You need to sign in to do that
Don't have an account?
Simon234
Test for HttpPost service: how to take JSON body for test?
I have a HttpPost methods. And I really don't know how to test it correctly.
My methods:
My test (just basis):
In App__c j = Endpoint.ParseRequest(request); I become Argument cannot be null. without coverage of:
My methods:
public static App__c ParseRequest(RestRequest req) { App__c app = new App__c(); String body = req.requestBody.toString(); app = (App__c)JSON.deserialize(body, App__c.class); return app; } @HttpPost global static Id doPost() { RestRequest req = RestContext.request; App__c app = ParseRequest(req); insert app; return app.id; }
My test (just basis):
public static String GenerateJSON(App__c a){ Map<String, String> fieldMap = new Map<String, String>{'Title__c' => a.Name}; String serialized = JSON.serialize(fieldMap); return serialized; } @isTest static void testPost() { App__c record = createTestRecord(); RestRequest request = new RestRequest(); request.requestUri = System.URL.getSalesforceBaseUrl().toExternalForm() + '/services/apexrest/App__c/' + record.Id; request.httpMethod = 'POST'; RestContext.request = request; record = (App__c)JSON.deserialize(GenerateJSON(record), App__c.class); App__c j = Endpoint.ParseRequest(request); record = (App__c)JSON.deserialize(body, App__c.class); } static App__c createTestRecord() { App__c app = new App__c( Title__c = 'Title', ); insert app; return app; }
In App__c j = Endpoint.ParseRequest(request); I become Argument cannot be null. without coverage of:
app = (App__c)JSON.deserialize(body, App__c.class); return app; } @HttpPost global static Id doPost() { RestRequest req = RestContext.request; App__c app = ParseRequest(req); insert app; return app.id; }
Thanks
Gulshan Raj