You need to sign in to do that
Don't have an account?
Kamil Mieczakowski
callout mock
I am attempting to test the following callout method:
And here's the test:
However, when I run this test from the classes menu in 'Setup' I get the following error and no test coverage:
/****** 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(); req.setMethod('GET'); //set method as containing the endpoint + companyID req.setEndpoint(endpoint+companyID); //set timeout req.setTimeout(120000); httpResponse res = new http().send(req); return res; }
And here's the test:
@isTest private class MMarkAPITest { static testMethod void MMarkAPIUnitTest() { test.startTest(); test.setMock(HttpCalloutMock.class, new MMarkAPITestHttpCalloutMock()); // Call method to test. // This causes a fake response to be sent // from the class that implements HttpCalloutMock. MMarkAPIcallout('company.com'); test.stopTest(); } }
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
However, when I choose Run Test from the Setup menu (Setup -> Apex Classes --> MMarkAPITest --> Run Test I get this:
Anonymous window. You should be running your Test Classes following the method given below -Setup --> Develop --> Apex Test Execution --> Select Tests
Please run your test this way and get me the error that you get under the Result column.
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
response.setHeader('');
response.setBody(body);
response.setStatusCode(200);
return response;
}
}
// test class implementation
Test.startTest();
Test.setMock(HttpCalloutMock.class, new WebServiceMockTest());
String domain = 'abc';
MMarkAPIcallout(domain);
test.stopTest();