1

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', '[email protected]');
        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...

3
  • The error happens when you just run SFLoginCtrlrTest? And the API versions are all recent?
    – Keith C
    Commented Mar 16, 2016 at 11:40
  • yes...the error comes on running the test class...and API versions are new (36.0 - Salesforce API)
    – ipradhansk
    Commented Mar 16, 2016 at 12:09
  • I am able to run your code without any issue. can you double check.
    – Himanshu
    Commented Mar 16, 2016 at 13:32

1 Answer 1

2

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', '[email protected]');
        logincred.put('password', 'notapassword');

        SFLoginCtrlr.getResp(JSON.serialize(logincred));

        Test.stopTest();
    }
}

so perhaps some unexpected side-effect of the @isTest on the mock?

1
  • great! worked for me too :) i tried removing @isTest from mock class but it failed anyways...but this one works...thanks :)
    – ipradhansk
    Commented Mar 16, 2016 at 12:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .