You need to sign in to do that
Don't have an account?
"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:
Code for AnimalLocatorMock Mock Class
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
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
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)
Best regards
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
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