You are here:
Archive Apex Testing
Before working in your production environment, test your Apex Class Wrapper in Archive Apex in the Archive app.
MockHttpResponseGenerator.cls
This code depicts the mock for the callout. The
MockHttpResponseGenerator class implements the
HttpCalloutMock Apex interface.
@isTest
global class MockHttpResponseGenerator implements HttpCalloutMock {
public String fakeResponseJson;
public integer statusCode;
String redirectHeader;
public MockHttpResponseGenerator(String fakeResponseJson,Integer statusCode){
this.statusCode = statusCode;
this.fakeResponseJson = fakeResponseJson;
this.redirectHeader = '/example/test';
}
public HTTPResponse respond(HTTPRequest req) {
HTTPResponse res = new HTTPResponse();
res.setBody(this.fakeResponseJson);
res.setStatusCode(this.statusCode);
res.setHeader('Location', this.redirectHeader);
return res;
}
}SearchSdkTest.cls
This code tests the global search functionality of the
SF_Archive.ArchiverAccessor by setting up a mock HTTP response using the
MockHttpResponseGenerator class. This test verifies that the
search operation returns the expected JSON data and status code.
@isTest
public with sharing class SearchSdkTest {
@isTest
private static void searchSdk() {
String jsonBody = '{"records":[{"CaseNumber":"01916767","Id":"500Xx000000XXXXXX"}],"total_result_count":1,"scroll_id":"-1"}';
SF_Archive.ArchiverAccessor.setMock(new MockHttpResponseGenerator(
jsonBody, SF_Archive.ArchiverAccessorResponse.HTTP_200_OK));
List<SF_Archive.SearchFilter> filters = new List<SF_Archive.SearchFilter>{
new SF_Archive.SearchFilter('Name', 'test')
};
Test.startTest();
SF_Archive.ArchiverAccessorResponse res =
SF_Archive.ArchiverAccessor.performArchiverGlobalSearch('Account', filters);
Test.stopTest();
System.assertEquals(SF_Archive.ArchiverAccessorResponse.HTTP_200_OK, res.getStatusCode());
System.assertEquals(jsonBody, res.getBody());
}
}