1

I have an apex class which contains code only for web callouts and for this class I get coverage equivalent to 9% since actual web callouts are not a part of tests. So how am I supposed to increase code coverage for this class? I am not sure whether this % will allow the code to be deployed on PROD from Sandbox.

Basically I need to move 2 classes and 1 trigger to PROD with coverage as:

  • Trigger - 89%
  • Class 1 - 67%
  • Class 2 (with Web callouts) - 9%
2
  • Just out of curiosity, any reason why writing unit tests with coverage does not work? – kurunve Dec 9 '19 at 10:42
  • Unit tests are working and giving the mentioned coverage but since one of my class contains code only for web callout, so this code does not have much coverage. Do I need to create mock callouts and how will they help in increasing coverage of that class? – Ashna Arora Dec 9 '19 at 10:44
2

Use apex mocks to achieve the goal. Apex mock is used to get a response from the callout you need. So when you are doing a callout

HttpRequest req = new HttpRequest();
req.setEndpoint('http://example.com/example/test');
req.setMethod('GET');
Http h = new Http();
HttpResponse res = h.send(req);

the last line will basically return the result you want. To increase code coverage for method with if/else conditions you just need to create few mocks as inner classes which will return the result needed to get into this condition blocks. Salesforce have great guide for that.

1
  • 1
    Thank you! I will try this. – Ashna Arora Dec 9 '19 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.