Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @isTest static void testMethodOne() {
- Test.StartTest();
- Test.setMock(HttpCalloutMock.class, new SFEndpointMock());
- SFEndpointMock.indicatorVar ='success';
- Test.StopTest();
- myClass.performRequest('testData1');
- }
- @isTest static void testMethodTwo() {
- Test.StartTest();
- Test.setMock(HttpCalloutMock.class, new SFEndpointMock());
- SFEndpointMock.indicatorVar ='success';
- Test.StopTest();
- myClass.performRequest('testData2');
- }
- @isTest
- global class SFEndpointMock implements HttpCalloutMock {
- public static String indicatorVar;
- global HttpResponse respond(HTTPRequest req){
- HttpResponse res = new HttpResponse();
- if(indicatorVar== 'success') {
- res.setStatus('OK');
- res.setStatusCode(200);
- String json = ***JSON BODY HERE***
- res.setBody(json);
- }
- else if (indicatorVar== 'success2') {
- res.setStatus('OK');
- res.setStatusCode(400);
- String json = ***DIFFERENT JSON BODY HERE***
- res.setBody(json);
- }
- return res;
- if(!Test.isRunningTest()) {
- res = http.send(request);
- }
- else {
- res= MockApiCalls.WebserviceExpectedResponse1();
- }
- public class MockApiCalls {
- public static HttpResponse WebserviceExpectedResponse1() {
- HttpResponse res = new HttpResponse();
- String json = ***JSON BODY HERE***
- res.setBody(json);
- res.setStatusCode(200);
- return res;
- }
- public static HttpResponse WebserviceExpectedResponse2() {
- HttpResponse res = new HttpResponse();
- String json = ***DIFFERENT JSON BODY HERE***
- res.setBody(json);
- res.setStatusCode(400);
- return res;
- }
- }
- @isTest global class SFEndpointMock implements HttpCalloutMock {
- private String indicatorVar;
- global SFEndpointMock(String indicatorVar) {
- this.indicatorVar = indicatorVar;
- }
- global HttpResponse respond(HTTPRequest req){
- ...
- }
- }
- Test.setMock(HttpCalloutMock.class, new SFEndpointMock('success'));
Add Comment
Please, Sign In to add comment