You need to sign in to do that
Don't have an account?
Ragnar Lothbrok 7
Test Class for Apex rest API Http POST
Hi there,
I have been working on some integration class, and i am new to integration things.
Below is my main class that reads the payload data and updates it, under that i try to created Test class for it but when i Run that test class it gives me an error like in .img.
There is also a Flow which invokes when Account record gets updated.
Main Class :
So, if anyone know anything, please help me in that. Any help is appreciated.
I have been working on some integration class, and i am new to integration things.
Below is my main class that reads the payload data and updates it, under that i try to created Test class for it but when i Run that test class it gives me an error like in .img.
There is also a Flow which invokes when Account record gets updated.
Main Class :
@RestResource(urlMapping='/accountInterestUpdate/*') global with sharing class accountInterestUpdate { @HttpPost global static void doPost() { Map<String, Object> requestBody= (Map<String, Object>)JSON.deserializeUntyped(RestContext.request.requestBody.toString()); Map<String, Object> requestBody1 = new Map<String, Object>(); for(String s: requestBody.keySet()){ requestBody1.put(s.toLowerCase(),requestBody.get(s)); } system.debug('requestBody' +requestBody); String buyerId = String.valueOf(requestBody1.get('buyerid')); String subStatus = String.valueOf(requestBody1.get('interested')); String buyerEmpId = String.valueOf(requestBody1.get('buyeremployeeid')); String subsId = String.valueOf(requestBody1.get('subscriptionid')); Account account = [SELECT ID, Name, API_Buyer_Id__c, Contact_Buyer_Id__c, Interested__c FROM Account WHERE API_Buyer_Id__c = :buyerId LIMIT 1]; if(subStatus == 'true'){ account.Interested__c = true; account.Contact_Buyer_Id__c = buyerEmpId; } else{ account.Interested__c = false; account.Contact_Buyer_Id__c = buyerEmpId; } update account; } }Test Class :
@isTest public class accountInterestUpdateTest { @isTest static void testPostMethod(){ Test.startTest(); Account acc = new Account(); acc.Name = 'Test Account'; acc.API_Buyer_Id__c = '234567'; acc.Interested__c = true; insert acc; RestRequest req = new RestRequest(); RestResponse res = new RestResponse(); req.requestURI = '/services/apexrest/accountInterestUpdate/'; String JSONMsg = '{"buyerId" : "'+acc.API_Buyer_Id__c+'","Interested":"true"}'; req.requestBody = Blob.valueof(JSONMsg); req.httpMethod = 'POST'; RestContext.request = req; RestContext.response= res; accountInterestUpdate.doPost(); Test.stopTest(); } @isTest static void testPostMethodInterestedFalse(){ Test.startTest(); Account acc = new Account(); acc.Name = 'Test Account'; acc.API_Buyer_Id__c = '234567'; insert acc; Contact con = new Contact(); con.FirstName = 'test'; con.LastName = 'contact'; con.AccountId = acc.id; insert con; RestRequest req = new RestRequest(); RestResponse res = new RestResponse(); req.requestURI = '/services/apexrest/accountInterestUpdate/'; String JSONMsg = '{"buyerId" : "'+acc.API_Buyer_Id__c+'","Interested":"false"}'; req.requestBody = Blob.valueof(JSONMsg); req.httpMethod = 'POST'; RestContext.request = req; RestContext.response= res; accountInterestUpdate.doPost(); Test.stopTest(); } }.img
So, if anyone know anything, please help me in that. Any help is appreciated.
As per the test class you are making account with Interested__c as true by which this flow is getting invoked. There is validation rule which tells that Soe fields are mandatory while creating the lead. Can you check on this Flow where the lead is created so based on it check if all required fields were getting mapped or not.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,