1

I have a platform event trigger that takes in a json string which contains an array of objects. My postman test call looks like this:

 "testCodeList__c":"[{\"testCode\":\"000000\",\"testName\":\"AAA\",\"procedureClass\":\"XX\"},{\"testCode\":\"111111\",\"testName\":\"BBB\",\"procedureClass\":\"YY\"},{\"testCode\":\"222222\",\"testName\":\"CCC",\"procedureClass\":\"ZZ\"}]"

(note that the quotes are each escaped with backslashes but when I submit this Stack Exchange post, it removes them)

My trigger uses json.deserializeUntyped like this:

List<Object> lstTestCodes = (List<Object>) JSON.deserializeUntyped(evt.testCodeList__c);

This trigger code works just fine, however I cannot get my test class to work. Inside the test class I am setting up a platform event with this parameter:

  testOrder.testCodeList__c = '[{"testCode":"000000","testName":"AAA","procedureClass":"XX"},{"testCode":"111111","testName":"BBB","procedureClass":"YY"},{"testCode":"222222","testName":"CCC","procedureClass":"ZZ"}]';

And when I run the test class, I get 'null input to JSON parser'. I have tried both escaping the quotes with backslash, and not escaping them, but I get the same problem. I have done a ton of Googling. Can anyone show me how I can set up my variable in my Apex test class? thanks!

2
  • Can you put log before this line and check JSON is properly populating there. Try to validate JSON on sites like jsonlint.com(I don't own the site, use at your discretion).
    – Ysr Shk
    Apr 14, 2021 at 12:12
  • From the error, it feels that in the JSON.deserializeUntyped the value is getting null
    – Ysr Shk
    Apr 14, 2021 at 12:13

1 Answer 1

0

I would make yourself a small inner class:

public class TestCode {
    public String testCode;
    public String testName;
    public String procedureClass;
}

Use it to both deserialize the incoming json:

TestCode[] codes = (TestCode[])JSON.deserialize(jsonString,TestCode[].class);

And generate the codes you need for your unit test :

new ParentClass.TestCode[] testCodeList = new ParentClass.TestCode[]{
  {'testCode':'000000','testName':'AAA','procedureClass':'XX'},
  {'testCode':'111111','testName':'BBB','procedureClass':'YY'},
  {'testCode':'222222','testName':'CCC','procedureClass':'ZZ'}
};
testOrder.testCodeList__c = JSON.serialize(testCodeList);

I have parent class prefixed above because you'd likely be calling the second code block from the unit test class (I'm assuming)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .