2

I was trying to test an apex class that uses some http callouts with HTTPMockRequest. When trying to run the Test class it gives me the following error:

Methods defined as TestMethod do not support Web service callouts

This is my Test class

@isTest()
public class retrieveGAdata_Test {


public static final string sDate = 'yesterday'; 
public static final string eDate = 'today'; 



  public static testmethod  void testsaveDataToSF()
  {

   retrieveGaData test1 = new retrieveGAdata();

     SingleRequestMock fakeAccountResp = new SingleRequestMock(200,
                                                     'Complete',
                                                     '[{"Name": "sForceTest1"}]',
                                                     null);

    SingleRequestMock fakeContactsResp = new SingleRequestMock(200,
                                              'Complete',
                                              '[{"LastName": "Test Last Name"}]',
                                               null);

    Map<String, HttpCalloutMock> endpoint2TestResp =
                               new Map<String,HttpCalloutMock>();
    endpoint2TestResp.put('https://accounts.google.com/o/oauth2/token',fakeAccountResp);
    endpoint2TestResp.put('https://www.googleapis.com/analytics/v3/data/',fakeContactsResp);

    HttpCalloutMock multiCalloutMock =
                               new MultiRequestMock(endpoint2TestResp);

    Test.setMock(HttpCalloutMock.class, multiCalloutMock);

  }

}
2

Put retrieveGaData test1 = new retrieveGAdata(); after Test.setMock. That's the only line of code that could execute a callout as far as I can see.

public static testmethod  void testsaveDataToSF()
{
    // other setup
    Test.setMock(HttpCalloutMock.class, multiCalloutMock);
    retrieveGaData test1 = new retrieveGAdata();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.