function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RajanRajan 

MockTesthelp

Hi friends,
I am having problem for code coverage. My class and Test classes are as below:
Class:
********************************
global with sharing class NavikUtilityClass {
    global Static HttpResponse callSendToNavikAuthentication(String endURL,String methodType,String methodFunctional,String accNum, String contact, String recommendation,Integer dataId, List<SendToInbox.response> input,List<String> dataIds) {
        String baseUrl = EndPoint__c.getvalues('BaseURL').EndPointURL__c;
        navikAuthDomain.response mapResp = navikAuthentication.getMapId(UserInfo.getUserEmail());
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setHeader('sessionToken',mapResp.sessionToken);
        request.setMethod(methodType);
        if(methodFunctional == 'ACTIVITY'){
            request.setEndpoint(baseUrl + '/' + endURL);
        }
        
       
        if(methodFunctional == 'DRILLDOWN'){
            request.setEndpoint(baseUrl + '/' + endURL);
            String accNo = EncodingUtil.urlEncode(accNum,'UTF-8');
            String cont = EncodingUtil.urlEncode(contact,'UTF-8');
            String recom = EncodingUtil.urlEncode(recommendation,'UTF-8');
            request.setBody('accountNumber='+accNo+'&contact='+cont+'&recommendation='+recom);
        }
        if(methodFunctional == 'SPECIFICRECOMM'){
            request.setHeader('content-type', 'application/json');
            request.setHeader('Accept', 'application/json');
            request.setEndpoint(baseUrl + '/' + endURL);
            request.setEndpoint(baseUrl + '/'+endURL+dataId);
        }
        if(methodFunctional == 'SENDEMAIL'){
            request.setHeader('content-type', 'application/json');
            request.setHeader('Accept', 'application/json');
            request.setEndpoint(baseUrl + '/' + endURL);
            String s = JSON.serialize(input);
            request.setBody(s);
        }
        if(methodFunctional == 'INVALID'){
            request.setHeader('content-type', 'application/json');
            request.setHeader('Accept', 'application/json');
            request.setEndpoint(baseUrl + '/' + endURL);
            request.setBody(JSON.serializePretty(dataIds));
        }
        HttpResponse response = null;
         try {
                response = http.send(request);
            } catch(System.CalloutException e) {
                System.debug('Callout error: '+ e);
                System.debug(response.toString());
            }
        return response;  
    }   
}

********
Test class
*********************
@isTest
public class NavikUtilityClassTest{
    public static testmethod void utilitytest() {
        List<String> dataIds;
        dataIds.add('abcd');
        Test.startTest();
        Test.setMock(HttpCalloutMock.class, new NavikUtilityClassMocGenerator());
        HttpResponse res = NavikUtilityClass.callSendToNavikAuthentication('http://api.salesforce.com/foo/bar', 'Testmethod', 'abc', '5142433683738', 'allen', 'recom', 27524, Null, dataIds);
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = res.getBody();
        String expectedValue = '{"foo":"bar"}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, res.getStatusCode());
    Test.stopTest();
    }
   

}
RajanRajan
My mockgenerator class is as below:
******************************

@isTest
global class NavikUtilityClassMocGenerator implements HttpCalloutMock {

    global HTTPResponse respond(HTTPRequest req) {
            // Optionally, only send a mock response for a specific endpoint
            // and method.
        System.assertEquals('http://api.salesforce.com/foo/bar', req.getEndpoint());
        System.assertEquals('GET', req.getMethod());
            // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"foo":"bar"}');
        res.setStatusCode(200);
        return res;
    }
}
VivekShindeVivekShinde
You need to call the "callSendToNavikAuthentication" method multiple times from the test methods with different values for the parameter "methodFunctional". You need to pass different values (DRILLDOWN, SPECIFICRECOMM, etc) of the parameter to cover the different if blocks.
RajanRajan
Hi Vivek,
Thank you for response.
Can you write code for atleast one block because I am new in testing and little confused for flow. It will be great help for me from your side if you will write. Advance thank you for the help and code
VivekShindeVivekShinde
@isTest
public class NavikUtilityClassTest{
    public static testmethod void utilitytest() {
        List<String> dataIds;
        dataIds.add('abcd');
        Test.startTest();
        Test.setMock(HttpCalloutMock.class, new NavikUtilityClassMocGenerator());
        HttpResponse res = 
       NavikUtilityClass.callSendToNavikAuthentication('http://api.salesforce.com/foo/bar', 'POST', 'DRILLDOWN', '5142433683738', 'allen', 'recom', 27524, Null, dataIds);
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = res.getBody();
        String expectedValue = '{"foo":"bar"}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, res.getStatusCode());
        Test.stopTest();
    }
    
    //Add another test method which will passing a different value other than
    //DRILLDOWN to the cassSendToNavikAuthentication method
}

Hope this helps you!
RajanRajan
Hi Vivek,
Thank you for help but still I am getting same error.
Error:-   Methods defined as TestMethod do not support Web service callouts

How to solve this?