0

I'm trying to write the test class for a method like this.

    @AuraEnabled  
public static DynamicTableMapping getFilteredAccounts(String filterId){
        List<string> headervalue = new List<string>();
        String nombreLista = [SELECT Id, Name FROM ListView WHERE Id = :filterId].Name;
        HttpRequest req = new HttpRequest(); 
        String baseUrl = URL.getSalesforceBaseUrl().toExternalForm(); 
        String endPoinURL = baseUrl+'/services/data/v45.0/sobjects/Account/listviews/'+filterId+'/describe'; 
        req.setEndpoint(endPoinURL);
        req.setMethod('GET');
        req.setHeader('Authorization',  'Bearer ' + UserInfo.getSessionId());
        Http http = new Http();
        HTTPResponse response = http.send(req); 
        Map<String, Object> tokenResponse = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
        String query = (String) tokenResponse.get('query');
        System.debug('query --> ' + query);
        for(string s:query.split(',')){
            s = s.trim();    
            if(s.startsWith('SELECT')){
                headervalue.add((s.removeStart('SELECT')).trim());        
            }else if(!s.startsWith('SYSTEMMODSTAMP') && !s.equalsIgnoreCase('SYSTEMMODSTAMP FROM ACCOUNT ORDER BY NAME ASC NULLS FIRST') && !s.contains('ASC')){
                headervalue.add(s.trim());
            }
        }
        return new DynamicTableMapping(headervalue);
}

The problem is that I'm getting this error: Method defined as testmethod does not support web service callouts. I've read this answer but I dont understand how to implement the solution: Method defined as testMethod do not support web service callouts, test skipped

What can I do to pass the cover for my method? My method DOESN'T RETURN a http response so I don't know how to make the mock properly. All examples I've found returns a http response...

Thanks in advance

3
  • My method doesn't return a http response so I don't know how to make the mock properly.
    – Javier
    Commented Jun 4, 2019 at 6:26
  • 1
    Can you try this and write a test class and see if it works. trailhead.salesforce.com/en/content/learn/modules/… Commented Jun 4, 2019 at 9:13
  • Your method does not need to return an HTTP response to use a Mock. The Mock is responsible for providing the return value that comes back to your code from http.send().
    – David Reed
    Commented Jun 4, 2019 at 13:35

0

Browse other questions tagged .