I am testing some HTTP callouts in Apex and instead of passing a newly created MyHttpCalloutMockImpl object in the test method every time, thought of declaring the same at class level and reusing it in each test method that tests a callout.
But this fails to compile stating "Variable does not exist". What is the reason for this? Here goes my code:
@isTest(seeAllData = false)
public class MyControllerTest {
HttpCalloutMockImpl mockImpl = new HttpCalloutMockImpl();
@testSetup static void setup() {
//insert some setup data
}
public static testMethod void testMyMethod1() {
Test.startTest();
MyController ctrlObj = new MyController();
Test.setMock(HttpCalloutMock.class, mockImpl);
ctrlObj.sync1();
Test.stopTest();
}
public static testMethod void testMyMethod2() {
Test.startTest();
MyController ctrlObj = new MyController();
Test.setMock(HttpCalloutMock.class, mockImpl);
ctrlObj.sync2();
Test.stopTest();
}
}