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', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b2f3e2626320b3e382e3965282426">[email protected]</a>'); 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…
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
This passes fine for me:
@isTest private class SFLoginCtrlrTest{ private class ExampleCalloutMock implements HttpCalloutMock{ public HttpResponse respond(HTTPRequest req){ HttpResponse res = new HttpResponse(); res.setStatus('OK'); res.setStatusCode(200); res.setBody('SUCCESS'); return res; } } static testMethod void SFLoginCtrlrTestMethod(){ Test.startTest(); Test.setMock(HttpCalloutMock.class, new ExampleCalloutMock()); Map<String, String> logincred = new Map<String, String>(); logincred.put('username', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8febfae2e2f6cffafceafda1ece0e2">[email protected]</a>'); logincred.put('password', 'notapassword'); SFLoginCtrlr.getResp(JSON.serialize(logincred)); Test.stopTest(); } }
so perhaps some unexpected side-effect of the @isTest
on the mock?
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0