This may sound repititve question. But the error incurs after following multiple links found like: here and here.
The controller:
public with sharing class SFLoginCtrlr {
public static HTTPResponse getResp(String loginCred){
Map<String, Object> mapLoginCred = (Map<String, Object>)JSON.deserializeUnTyped(loginCred);
String username = ''+mapLoginCred.get('username');
String password = ''+mapLoginCred.get('password');
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setTimeout(60000);
req.setEndpoint('https://www.salesforce.com/services/Soap/u/29.0');
req.setHeader('Content-Type', 'text/xml;charset=UTF-8');
req.setHeader('SOAPAction', '""');
req.setBody('<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Header/><Body><login xmlns="urn:partner.soap.sforce.com"><username>' +username+ '</username><password>' + password + '</password></login></Body></Envelope>');
HttpResponse res = new Http().send(req);
return res;
}
}
The mock class:
@isTest
global class ExampleCalloutMock implements HttpCalloutMock{
global HttpResponse respond(HTTPRequest req){
HttpResponse res = new HttpResponse();
res.setStatus('OK');
res.setStatusCode(200);
res.setBody('SUCCESS');
return res;
}
}
The test class:
@isTest
private class SFLoginCtrlrTest{
static testMethod void SFLoginCtrlrTestMethod(){
Test.startTest();
Test.setMock(HttpCalloutMock.class, new ExampleCalloutMock());
Map<String, String> logincred = new Map<String, String>();
logincred.put('username', 'dummy@user.com');
logincred.put('password', 'notapassword');
SFLoginCtrlr.getResp(JSON.serialize(logincred));
Test.stopTest();
}
}
The error received while running test class:
Methods defined as TestMethod do not support Web service callouts
Not sure, where is the issue...