+ Start a Discussion
Kamil MieczakowskiKamil Mieczakowski 

callout mock

I am attempting to test the following callout method:
/******  CALLOUT  ******/
      public static HttpResponse MMarkAPIcallout(String domain){
        string endpoint = 'https://someapi.com/api/v5/company/dd/det?comp_id=';

        //get companyID using domain (this is a separate callout)
        string companyID = companyIDFetcher.companyIDFetcher(domain);

        //system.debug('companyID is' +companyID);
        httpRequest req = new httpRequest();


//set method as containing the endpoint + companyID

//set timeout

        httpResponse res = new http().send(req);

        return res;

And here's the test:
private class MMarkAPITest {

    static testMethod void MMarkAPIUnitTest() {
        test.setMock(HttpCalloutMock.class, new MMarkAPITestHttpCalloutMock());

        // Call method to test.
        // This causes a fake response to be sent
        // from the class that implements HttpCalloutMock.



However, when I run this test from the classes menu in 'Setup' I get the following error and no test coverage:
system.nullpointerexception attempt to dereference a null object salesforce

Shruti SShruti S
Could you please give me the line in which the error occurs?
Kamil MieczakowskiKamil Mieczakowski
Hi Shruti and thank you for your reply. When I am trying to run the code from the anonymous window I get this error (Btw, the above error persists even when I change the class to 'public'):
Line: 19, Column: 1
Method is not visible: MMarkAPITest.MMarkAPIUnitTest()

However, when I choose Run Test from the Setup menu (Setup -> Apex Classes --> MMarkAPITest --> Run Test I get this:
system.nullpointerexception attempt to dereference a null object salesforce

Shruti SShruti S
You cannot call a Test Class from an Anonymous window. You should be running your Test Classes following the method given below -  

Setup --> Develop --> Apex Test Execution --> Select Tests

User-added image

User-added image

Please run your test this way and get me the error that you get under the Result column.
Kamil MieczakowskiKamil Mieczakowski
Shruti, thank you for this. I did run the test for the first time following the same method that you described above. That's when the error described in my first post occured. Here's the full message:
Time Started	6/9/2017 8:17 AM
Class	MMarkAPITest
Method Name	MMarkAPIUnitTest
Pass/Fail	Fail
Error Message	System.NullPointerException: Attempt to de-reference a null object
Stack Trace	Class.MMarkAPITestHttpCalloutMock.respond: line 12, column 1
Class.companyIDFetcher.companyIDFetcher: line 12, column 1
Class.MMarkAPI.MMarkAPIcallout: line 8, column 1
Class.MMarkAPITest.MMarkAPIUnitTest: line 11, column 1
Kiran SKiran S

Hi Kamil,


please try to imlement as per below :


// fake response as per your api response format
global class WebServiceMockTest implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest request) {

      HttpResponse response = new HttpResponse();
      String body = '';  // content as per your api response
        return response; 

// test class implementation

 Test.setMock(HttpCalloutMock.class, new WebServiceMockTest()); 
 String domain = 'abc';