You need to sign in to do that
Don't have an account?

Compile Error: Missing '<EOF>' at '@' at line 21 column 1
public class AnimalLocator
{
public static String getAnimalNameById(Integer id)
{
String returnval=null;
Http http=new Http();
HttpRequest request = new HttpRequest();
request.setEndPoint('https://th-apex-http-callout.herokuapp.com/animals/id'+id);
request.setMethod('GET');
HttpResponse response=http.send(request);
if(response.getStatusCode()==200)
{
Map<String,Object>jsonbody=(Map<String,Object>)JSON.deserializeUntyped(response.getBody());
Map<String,Object>result=(Map<String,Object>)jsonbody.get('animal');
returnval=String.valueof(result.get('name'));
}
return retunvalue;
}
}
@isTest
global class AnimalLocatorMock implements HttpCalloutMock
{
global HttpResponse respond(HttpRequest request)
{
HttpResponse response=new Httpresponse();
response.setheader('Content-Type','application/json');
response.setbody('{"animal":{"id":1,"name":"tiger","eats":"deer","says":"roar"}}');
response.setStatusCode(200);
return response;
}
}
@isTest
private class AnimalLocatorTest()
{
@isTest static void testAnimalNamebyId()
{
Test.setMock(HttpCalloutMock.class,new AnimalLocatorMock());
String result= AnimalLocator.getAnimalNameById(1);
system.assertNotEquals(null,result,'the callout returns a null response');
system.assetEquals('tiger',result);
}
}
{
public static String getAnimalNameById(Integer id)
{
String returnval=null;
Http http=new Http();
HttpRequest request = new HttpRequest();
request.setEndPoint('https://th-apex-http-callout.herokuapp.com/animals/id'+id);
request.setMethod('GET');
HttpResponse response=http.send(request);
if(response.getStatusCode()==200)
{
Map<String,Object>jsonbody=(Map<String,Object>)JSON.deserializeUntyped(response.getBody());
Map<String,Object>result=(Map<String,Object>)jsonbody.get('animal');
returnval=String.valueof(result.get('name'));
}
return retunvalue;
}
}
@isTest
global class AnimalLocatorMock implements HttpCalloutMock
{
global HttpResponse respond(HttpRequest request)
{
HttpResponse response=new Httpresponse();
response.setheader('Content-Type','application/json');
response.setbody('{"animal":{"id":1,"name":"tiger","eats":"deer","says":"roar"}}');
response.setStatusCode(200);
return response;
}
}
@isTest
private class AnimalLocatorTest()
{
@isTest static void testAnimalNamebyId()
{
Test.setMock(HttpCalloutMock.class,new AnimalLocatorMock());
String result= AnimalLocator.getAnimalNameById(1);
system.assertNotEquals(null,result,'the callout returns a null response');
system.assetEquals('tiger',result);
}
}
I see that your query is related to the trailhead module
Apex Integration Services . Can you check if you have created a separate class for the test class AnimalLocatorMock ?
The link https://gist.github.com/mannharleen/2890d40f5cea62d058d62f7c16e39f73 would be helpful.
If you have any additional queries, we have a separate Trailhead team who can help you with these issues. You can use the below link to reach out to them so that one of the Engineers will get in touch with you.
Support: https://trailhead.salesforce.com/en/help?support=home
Please mark this answer as best if it helps so that others facing the same issue will find it useful. Thanks
Hi Aryan,
These need to be stored as different classes
Class 1 - AnimalLocator
Class 2 - AnimalLocatorMock And finally, the test class - AnimalLocatorTest
This should fix your problem.