function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andy Kallio 7Andy Kallio 7 

Admin needs help Testing integration

Hello,
I'm an admin that has just written my first integration. The code makes a call to an external service, receives some json, and then parses it and places it into a custom object. The json could either contain new records or update an existing record from a previous run...this is being done with upsert dml statement.  This process runs on a schedule. 

My question is regarding testing. There are no problems with testing the insert of new records. There is a test method that sets up all of the needed test data and a json string that makes use of that test data. The method returns that string and is used as a mockHTTPResponse. This tests everything except for the branch of code that updates existing records. This is where I'm challenged...I don't see how I can make another json string that uses first set of test data. 

So, I'm hoping that someone out there has been through this before and can offers some suggestions. 

Best Answer chosen by Andy Kallio 7
Abdul KhatriAbdul Khatri
This is an http callout to an external service that returns a response in the form of json, whose information is getting upserted into your custom object. Since you already mentioned having a test method that make use of the the mockHTTPResponse which I am not sure you are using for insert or for update but let say if that use of Insert, you can create another mock response by adding a key field used to upsert the record but before that insert the record manually in the custom object.

I am presuming your mock class name is mockHTTPResponse, you can create another class like mockHTTPUpdateResponse and add the key value in the response with the value already existed.

I hope I understood your need correctly and was able to make my point. If not give me some more details with example. Thanks. 

All Answers

Abdul KhatriAbdul Khatri
This is an http callout to an external service that returns a response in the form of json, whose information is getting upserted into your custom object. Since you already mentioned having a test method that make use of the the mockHTTPResponse which I am not sure you are using for insert or for update but let say if that use of Insert, you can create another mock response by adding a key field used to upsert the record but before that insert the record manually in the custom object.

I am presuming your mock class name is mockHTTPResponse, you can create another class like mockHTTPUpdateResponse and add the key value in the response with the value already existed.

I hope I understood your need correctly and was able to make my point. If not give me some more details with example. Thanks. 
This was selected as the best answer
Andy Kallio 7Andy Kallio 7
Thanks!!! I think all I need to do is add to my method that sets up the test data to insert some records into the object that the json is parsed into and ensuring that they have some matching keys as the json for the mockHTTPResponse. 
Abdul KhatriAbdul Khatri
you can use @testSetup method for all your DML Operations. All Data inserted in that method will be available throught you class in all other methods. Here is the link for your reference.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm