1
createFolderResponse = SharePoint_IntegrationHandler.createFolder(null,parentFolder);
if(createFolderResponse.getStatusCode() ==  200 || createFolderResponse.getStatusCode() ==  201){

    //----------- Uploading file -------------//

    HttpResponse fileUploadResponse = SharePoint_IntegrationHandler.uploadDoc(null,parentFolder,objAttach.Name, objAttach.Body);

    if(fileUploadResponse.getStatusCode() ==  200 || fileUploadResponse.getStatusCode() ==  201){
        lstAttachemntToDelete.add(objAttach);
        objDoc.Share_Point_Location__c = parentFolder + '/' + objAttach.Name;
        objDoc.File_Name__c = objAttach.Name;
        objDoc.Has_Temp_Attachment__c = false;
        //objDoc.Share_Point_URL__c = ;
        objDoc.Share_Point_API_Response__c = 'uploaded successfully';
    }

    else{
        objDoc.Share_Point_API_Response__c = ' Status = '+ fileUploadResponse.getStatus();
        objDoc.Share_Point_API_Response__c += ' Status Code = '+  fileUploadResponse.getStatusCode() + '\n';
        string resBody = fileUploadResponse.getBody();
        if(resBody.length() > 200){
            resBody = resBody.substring(0,200);
        }
        objDoc.Share_Point_API_Response__c += ' Response : '+ resBody;
    }
}
else{
    objDoc.Share_Point_API_Response__c = ' Status = '+ createFolderResponse.getStatus();
    objDoc.Share_Point_API_Response__c += ' Status Code = '+ createFolderResponse.getStatusCode();

    string resBody = createFolderResponse.getBody();
    if(resBody.length() > 200){
        resBody = resBody.substring(0,200);
    }
    objDoc.Share_Point_API_Response__c += ' Response : '+ resBody;
}

The SharePoint_IntegrationHandler class methods do the actual callouts. Creating a fake response or testing with mock call out either covers the if or the else part.

This is my structure in short. Either both the if conditions are covered or both the else ones.

if
{
    if{
    }
    else{
    }
}
else{
}

Any help would be appreciated. Thanks.

5

Write test-method for each possible scenario. For example, you have the following conditions:

if(A) {
    if(subA) {
        do A and subA;
    } else {
       do A and not SubA;
    }
} else {
    do not A;
}

to cover all lines of this condition:

@isTest
static void test_do_A_and_subA(){
    setup A and subA test data;
    invoke method to test;
    check asserts;
}

@isTest
static void test_do_A_and_not_SubA(){
    setup A and not subA test data;
    invoke method to test;
    check asserts;
}

@isTest
static void test_do_not_A(){
    setup not A test data;
    invoke method to test;
    check asserts;
}

Applying it to how to test Http callouts, for each test-method you can set different status codes, that are passed to mock by constructor parameter.

global class MockHttpResponse implements HttpCalloutMock {
    private Integer statusCode;

    public MockHttpResponse(Integer statusCode){
        this.statusCode = statusCode;
    }

    global HTTPResponse respond(HTTPRequest req) {
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"foo":"bar"}');
        res.setStatusCode(this.statusCode);
        return res;
    }
}

@isTest
static void test_do_A_and_subA(){
    setup A and subA test data;
    Test.setMock(HttpCalloutMock.class, new MockHttpResponse(200)); //setup mock with desired 200 status code
    invoke method to test;
    check asserts;
}

@isTest
static void test_do_A_and_not_SubA(){
    setup A and not subA test data;
    Test.setMock(HttpCalloutMock.class, new MockHttpResponse(404)); //setup mock with desired 404 status code
    invoke method to test;
    check asserts;
}

@isTest
static void test_do_not_A(){
    setup not A test data;
    Test.setMock(HttpCalloutMock.class, new MockHttpResponse(500)); //setup mock with desired 500 status code
    invoke method to test;
    check asserts;
}
1
  • thanx @alexander. That worked for me..!! – Chayan Jul 21 '17 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.