+ Start a Discussion
Veena GopalVeena Gopal 

rest callout trailhead test using static resource

I am getting the following error for a particular line. This is just a example given in the trailhead rest callouts testclass using static resource.
Line:Test.setMock(HttpCalloutMock.class, mock);
Error:Method does not exist or incorrect signature: void setMock(System.Type, System.StaticResourceCalloutMock) from the type test

As given in other posts, i have checked if there is a duplicate class named StaticResourceCalloutMock but i do not have any.
also the name of the static resource i have mentioned in the test class is correct.
Please help.
Best Answer chosen by Veena Gopal
Shruti SShruti S
I was able to replicate this in my Org too. Take a look at the screenshot below - User-added image

This was because in my Org I had a class called Test. When I removed that class, the issue was solved.
public class Test {
}

All Answers

Shruti SShruti S
Do you mind posting the complete Test Class which you have written? It is a bit tricky looking at just two lines.
Veena GopalVeena Gopal
Hi Shruti,
It is a example from the trailhead. it is there in the trailhead itself. 

@isTest
private class AnimalsCalloutsTest {
@isTest static void testGetCallout() {
// Create the mock response based on a static resource
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); -----> this is the line which is giving error.
// Call method to test
HttpResponse result = AnimalsCallouts.makeGetCallout();
// Verify mock response is not null
System.assertNotEquals(null,result, 'The callout returned a null response.');
// Verify status code
System.assertEquals(200,result.getStatusCode(), 'The status code is not 200.');
// Verify content type
System.assertEquals('application/json;charset=UTF-8', result.getHeader('Content-Type'), 'The content type value is not expected.');
// Verify the array contains 3 items
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(result.getBody());
List<Object> animals = (List<Object>) results.get('animals'); System.assertEquals(3, animals.size(), 'The array should only contain 3 items.');
}
}
Shruti SShruti S
I was able to replicate this in my Org too. Take a look at the screenshot below - User-added image

This was because in my Org I had a class called Test. When I removed that class, the issue was solved.
public class Test {
}
This was selected as the best answer
Veena GopalVeena Gopal
Thank you Shruti