0

I am fetching the data of Account from the various Instances

My Apex controller:

public class Accounting{





    public  list<Account> lic{get;set;}
    public  list<Account> licUpdated{get;set;}

    Public String endPoint{get;set;}
    Public string str{get;set;}
    Public List<wrapperUserLic> wrapperList{get;set;}

    public Accounting()
    {
    wrapperList = new List<wrapperUserLic>();

    endPoint = '';
    str = '';

    requestres();

   }









public pageReference requestres() {        


        for(Salesforce_org__mdt  cust : [SELECT Id,instance__c, Label FROM Salesforce_insurance__mdt  ])
        {
                if(cust.Label!= 'None')
                {
                list<Account> lic = new  list<Account>();
                licUpdated = new list<Account>();
                HttpRequest req = new HttpRequest();                                        
                endPoint = 'callout:'+cust.Label+'/services/data/v39.0/query/?q=SELECT+Id,Name+FROM+Account';                
                req.setEndpoint(endPoint);                 
                req.setMethod('GET');



        //Get SessionId
        string autho = 'Bearer '+userInfo.getSessionId();
        req.setHeader('Authorization', autho);

        //Get Response
        Http http = new Http();
        HTTPresponse res= http.send(req);
        string response = res.getBody();
        system.debug('response:'+res.getbody());         
        //Deserialize obtained json response                 
        str = '['+ response.substring(response.indexOf('records":[')+10,response.indexof(']}')) +']';                     
        lic = (list<Account>)JSON.deserialize(str,list<Account>.class);

        wrapperUserLic wrapperuser = new wrapperUserLic(cust.Label , cust.instance__c , licUpdated); 
        wrapperList.add(wrapperuser) ;   
        }   
        }


        return null;
        }





        Public class wrapperUserLic
        {
            public string orgname{get;set;}
            Public String orgId{get;set;}
            Public List<Account> userAccountList{get;set;}

            public wrapperUserLic(String orgname , String orgId , List<Account> userAccountList)
            {
                this.orgName = orgName;
                this.orgId = orgId;
                this.userLicenseList = userAccountList;
            }

        }

}

My Test Class:

@isTest
private class AccountingTestClass {
     static testMethod void validateAccounting() {






      Accounting cc =new Accounting();


    CalloutClassTest hh = new CalloutClassTest();



         Test.setMock(HttpCalloutMock.class,hh);
         Test.startTest();
         cc.requestres();

        Test.StopTest();





    }
    }

Mock Response Class:

@isTest
global class CalloutClassTest implements HttpCalloutMock  {

global HTTPResponse respond(HTTPRequest req) {

 //Http http = new Http();
List<Account> license = [select Id,Name from Account where Label='test' Limit 1 ];  
HttpResponse res = new HttpResponse();
string jsonResBody = '{"totalSize":11,"done":true,"records":[{"attributes":{"type":"Account","url":"/services/data/v39.0/sobjects/Account/1002M000000bc8yQAA"}}}]
res.setbody(jsonResBody);
res.setStatusCode(200);
return res;

}
}

Error:Test method does not support webservice callout

1 Answer 1

0

Are you sure it's a rest endpoint, and not a soap endpoint? If it's a soap, you need to use webservicemocks, not httpmocks. According to the documentation:

When you create an Apex class from a WSDL, the methods in the autogenerated class call WebServiceCallout.invoke, which performs the callout to the external service. When testing these methods, you can instruct the Apex runtime to generate a fake response whenever WebServiceCallout.invoke is called. To do so, implement the WebServiceMock interface and specify a fake response for the testing runtime to send.

So the trailhead module below will show you how to write the doinvoke() method for the WebserviceMock

https://trailhead.salesforce.com/en/content/learn/modules/apex_integration_services/apex_integration_soap_callouts

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_interface_webservicemock.htm

3
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Apr 2, 2019 at 20:24
  • 1
    Got it. thanks @battery.cord I'll update it. Apr 2, 2019 at 20:25
  • @user11235813,can you explain which line i need to change in my code, i am not able to rectify the error
    – user55489
    Apr 4, 2019 at 13:12

You must log in to answer this question.