You need to sign in to do that
Don't have an account?
Rajan
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();
}
}
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();
}
}
******************************
@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;
}
}
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
Hope this helps you!
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?