[SalesForce] Passing a JSON parameter from test class to class method

I have a class which has a method with a parameter of JSON type. Now my goal is to create a Test class to test the method with the parameter of JSON. Please see my codes.

This is my method in my myClass:

public static void createAccount(CT_Part result){
  System.debug(result.record);
}

Here is my Test Class method:

@isTest static void createAccountTest(){

}

This are my sample Data JSON which I will use to test the method from my test class.

{'record':{'sourceSystemIdentifier':'System Administrator',
                    'sourceProcessIdentifier':null,
                    'initiatorAuthenticationLevel':'NA',
                    'correlationId':'sample',
                    'requestorId':'sample',
                    'requestorRole':'sample'}}

How will I test my method using the sample JSON data.

Best Answer

If I understood well, you have to deserialize your sample data into you object (using Json.Deserialize method), and then call your test function as usual

cf : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm#apex_class_System_Json

Related Topic