Activity Forums Salesforce® Discussions What is mock test class in Salesforce and is it important?

  • What is mock test class in Salesforce and is it important?

    Posted by Deepak on August 9, 2019 at 1:41 pm

    I am writing a test class, I have to hit a url and get some response. For this purpose how to write a mock test class and why?

    • This discussion was modified 4 years, 8 months ago by  Deepak.
    Yogesh replied 4 years, 9 months ago 2 Members · 1 Reply
  • 1 Reply
  • Yogesh

    Member
    August 9, 2019 at 2:50 pm

    To deploy or package Apex, 75% of your code must have test coverage. By default, test methods don’t support HTTP callouts, so tests that perform callouts fail. Enable HTTP callout testing by instructing Apex to generate mock responses in tests, using Test.setMock.

    example :-

    public class CalloutClass {
    public static HttpResponse getInfoFromExternalService() {
    HttpRequest req = new HttpRequest();
    req.setEndpoint('http://example.com/example/test');
    req.setMethod('GET');
    Http h = new Http();
    HttpResponse res = h.send(req);
    return res;
    }
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos