function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Karunat1Karunat1 

"Method does not exist or incorrect signature: void setMock(System.Type, myfirstcontapp.AnimalLocatorMock) from the type myfirstcontapp.test".

"myfirstcontapp" is a Namespace Prefix

Code for AnimalLocator Class:
public class AnimalLocator{
   public static String getAnimalNameById(Integer id){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+id);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        String strResp = '';
        system.debug('******response '+response.getStatusCode());
        system.debug('******response '+response.getBody());
        // If the request is successful, parse the JSON response.
        if (response.getStatusCode() == 200){
            // Deserializes the JSON string into collections of primitive data types.
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            // Cast the values in the 'animals' key as a list
            Map<string,object> animals = (map<string,object>) results.get('animal');
            System.debug('Received the following animals:' + animals );
            strResp = string.valueof(animals.get('name'));
            System.debug('strResp >>>>>>' + strResp );
        }
        return strResp ;
    }
}


Code for AnimalLocatorMock Mock Class
@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest request) {
        // Create a fake response
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}');
        response.setStatusCode(200);
        return response; 
    }
}
Code for AnimalLocatorTest​  Test Class:
@isTest
public class AnimalLocatorTest {
  @isTest public static void AnimalLocatorMock() {
       //Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
      test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
        string result = AnimalLocator.getAnimalNameById(1);
      system.debug(result);
        String expectedResult = 'chicken';
        System.assertEquals(result,expectedResult );
    }
}
Thanks
Best Answer chosen by Karunat1
Amit Chaudhary 8Amit Chaudhary 8
Look like issue is coming because of NameSpace.

Try to create new playGround and do the same coding in same org, I hope that will resolved your issue

Please check below post for same issue
1) https://developer.salesforce.com/forums/?id=9060G000000ICXQQA4

Let us know if this will help you
 

All Answers

Sampath SuranjiSampath Suranji
Hi,
You have to reffer the StaticResourceCalloutMock class for the fake response. Try with below code as given in the trailhead (https://trailhead.salesforce.com/modules/apex_integration_services/units/apex_integration_rest_callouts
@isTest
public class AnimalLocatorTest {
    
    @isTest public static  void getAnimalNameByIdTest()
    {
        Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock()); 
        
        StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
        mock.setStaticResource('GetAnimalResource');
        mock.setStatusCode(200);
        mock.setHeader('Content-Type', 'application/json;charset=UTF-8');
        // Associate the callout with a mock response
        Test.setMock(HttpCalloutMock.class, mock);
        
        string result= AnimalLocator.getAnimalNameById(1);
    }
}

Best regards
Amit Chaudhary 8Amit Chaudhary 8
Look like issue is coming because of NameSpace.

Try to create new playGround and do the same coding in same org, I hope that will resolved your issue

Please check below post for same issue
1) https://developer.salesforce.com/forums/?id=9060G000000ICXQQA4

Let us know if this will help you
 
This was selected as the best answer
Narender Singh(Nads)Narender Singh(Nads)
Hi Karuna,
Replace this line of code
test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());

with 
myfirstcontapp.test.setMock(myfirstcontapp.HttpCalloutMock.class, new AnimalLocatorMock()); 

If this doesn't work try the next one
Try this line:
myfirstcontapp.system.test.setMock(myfirstcontapp.system.HttpCalloutMock.class, new AnimalLocatorMock()); 

Am not sure if it will work but you can try.
Let me know if it helps.
Thanks
Karunat1Karunat1
@Narender it's not working
Karunat1Karunat1
Thanks Amit it is working in Trailhead Playground...
Sreenu Reddy 16Sreenu Reddy 16
 System.Test.setMock(WebServiceMock.class, new ParkServiceMock());
Allia Butool 5Allia Butool 5
Thanks Sreenu putting System. actually helped.