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
vikas rathi91vikas rathi91 

I am Cover my class 100% but Test class is fail.

MY  callout class 
public with sharing class XeroCalloutUtility {
    public static Xero_Settings__c xeroSettings {
        get {
            if (xeroSettings == null) {
                xeroSettings = Xero_Settings__c.getInstance('Xero');          
            }
            return xeroSettings;
        }
        set;
    }
    public static HttpResponse executeCallout (String method, String resourceName, String requestBody) {
        system.debug('>>>>>>>>>>xeroSettings '+xeroSettings);
        HttpRequest request = new HttpRequest();    
        request.setMethod(method);
        request.setEndpoint(xeroSettings.Endpoint__c + resourceName);
        request.setHeader('Accept', 'application/json');
        system.debug('>>>>>>>>>>>>>>>>>>request '+request);
        if (String.isNotBlank(requestBody)) {
            request.setBody(requestBody);           
            request.setHeader('Content-Type', 'text/xml');
        }  
        request = XeroOAuthUtility.signRequest(request, xeroSettings.Consumer_Key__c);        
        return new Http().send(request);
    }
}


Here is my test class 
@isTest
public with sharing class XeroCalloutUtilityTest{
        
     Public Static TestMethod Void Testcallout(){
             Test.StartTest();
             Xero_Settings__c xeroseting = new Xero_Settings__c();
             xeroseting.name = 'testName';
             xeroseting.Consumer_Key__c = 'testConsumer';
             xeroseting.Endpoint__c = 'www.xero.com';
             insert xeroseting;
         
       XeroCalloutUtility.executeCallout(' request.setMethod','request.setEndpoint','request.setBody');
       
        HttpRequest request =  new HttpRequest();
        String contentType = request.getHeader('Content-Type');
        String actualValue = request.getBody();
        request.setHeader('Accept', 'application/json');
        //HttpResponse response = new HTTP().send(request);
        Test.StopTest();     
     }  
}

Any one  help me  where I am wrong
Best Answer chosen by vikas rathi91
karthikeyan perumalkarthikeyan perumal
Hello 

you can't make a callout in test class.  create MokeCalloutRequest class. and call this fake callout class in you test class  then  will work. 


use below code for Create  SingleRequestMock class. 
First Create this class. 
@isTest
public class SingleRequestMock implements HttpCalloutMock {
        protected Integer code;
        protected String status;
        protected String bodyAsString;
        protected Blob bodyAsBlob;
        protected Map<String, String> responseHeaders;

        public SingleRequestMock(Integer code, String status, String body,
                                         Map<String, String> responseHeaders) {
            this.code = code;
            this.status = status;
            this.bodyAsString = body;
            this.bodyAsBlob = null;
            this.responseHeaders = responseHeaders;
        }

        public SingleRequestMock(Integer code, String status, Blob body,
                                         Map<String, String> responseHeaders) {
            this.code = code;
            this.status = status;
            this.bodyAsBlob = body;
            this.bodyAsString = null;
            this.responseHeaders = responseHeaders;
        }

        public HTTPResponse respond(HTTPRequest req) {
            HttpResponse resp = new HttpResponse();
            resp.setStatusCode(code);
            resp.setStatus(status);
            if (bodyAsBlob != null) {
                resp.setBodyAsBlob(bodyAsBlob);
            } else {
                resp.setBody(bodyAsString);
            }

            if (responseHeaders != null) {
                 for (String key : responseHeaders.keySet()) {
                resp.setHeader(key, responseHeaders.get(key));
                 }
            }
            return resp;
        }
}

use below upadted Testclass code.
 
@isTest(SeeAllData=true)

public with sharing class XeroCalloutUtilityTest{
        
     Public Static TestMethod Void Testcallout(){
             Test.StartTest();
             Xero_Settings__c xeroseting = new Xero_Settings__c();
             xeroseting.name = 'testName';
             xeroseting.Consumer_Key__c = 'testConsumer';
             xeroseting.Endpoint__c = 'www.xero.com';
             insert xeroseting;
       SingleRequestMock fakeResponse = new SingleRequestMock(200, 'Complete', '[{"Name": "sForceTest1"}]', null);
       Test.setMock(HttpCalloutMock.class, fakeResponse);  
       XeroCalloutUtility.executeCallout(' request.setMethod','request.setEndpoint','request.setBody');
       
        HttpRequest request =  new HttpRequest();
        String contentType = request.getHeader('Content-Type');
        String actualValue = request.getBody();
        request.setHeader('Accept', 'application/json');
        //HttpResponse response = new HTTP().send(request);
        Test.StopTest();     
     }  
}


Hope this will help you. 
Mark Best ANSWER if its work for you.
Thanks
Karthik
 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello 

you can't make a callout in test class.  create MokeCalloutRequest class. and call this fake callout class in you test class  then  will work. 


use below code for Create  SingleRequestMock class. 
First Create this class. 
@isTest
public class SingleRequestMock implements HttpCalloutMock {
        protected Integer code;
        protected String status;
        protected String bodyAsString;
        protected Blob bodyAsBlob;
        protected Map<String, String> responseHeaders;

        public SingleRequestMock(Integer code, String status, String body,
                                         Map<String, String> responseHeaders) {
            this.code = code;
            this.status = status;
            this.bodyAsString = body;
            this.bodyAsBlob = null;
            this.responseHeaders = responseHeaders;
        }

        public SingleRequestMock(Integer code, String status, Blob body,
                                         Map<String, String> responseHeaders) {
            this.code = code;
            this.status = status;
            this.bodyAsBlob = body;
            this.bodyAsString = null;
            this.responseHeaders = responseHeaders;
        }

        public HTTPResponse respond(HTTPRequest req) {
            HttpResponse resp = new HttpResponse();
            resp.setStatusCode(code);
            resp.setStatus(status);
            if (bodyAsBlob != null) {
                resp.setBodyAsBlob(bodyAsBlob);
            } else {
                resp.setBody(bodyAsString);
            }

            if (responseHeaders != null) {
                 for (String key : responseHeaders.keySet()) {
                resp.setHeader(key, responseHeaders.get(key));
                 }
            }
            return resp;
        }
}

use below upadted Testclass code.
 
@isTest(SeeAllData=true)

public with sharing class XeroCalloutUtilityTest{
        
     Public Static TestMethod Void Testcallout(){
             Test.StartTest();
             Xero_Settings__c xeroseting = new Xero_Settings__c();
             xeroseting.name = 'testName';
             xeroseting.Consumer_Key__c = 'testConsumer';
             xeroseting.Endpoint__c = 'www.xero.com';
             insert xeroseting;
       SingleRequestMock fakeResponse = new SingleRequestMock(200, 'Complete', '[{"Name": "sForceTest1"}]', null);
       Test.setMock(HttpCalloutMock.class, fakeResponse);  
       XeroCalloutUtility.executeCallout(' request.setMethod','request.setEndpoint','request.setBody');
       
        HttpRequest request =  new HttpRequest();
        String contentType = request.getHeader('Content-Type');
        String actualValue = request.getBody();
        request.setHeader('Accept', 'application/json');
        //HttpResponse response = new HTTP().send(request);
        Test.StopTest();     
     }  
}


Hope this will help you. 
Mark Best ANSWER if its work for you.
Thanks
Karthik
 
This was selected as the best answer
Naveen DhanarajNaveen Dhanaraj
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());

Create fake response, instruct the Apex runtime to send this fake response by calling Test.setMock in your test method.
vikas rathi91vikas rathi91
 @karthikeyan perumal 

its Showing the below error

You have uncommitted work pending. Please commit or rollback before calling out
karthikeyan perumalkarthikeyan perumal
Hello

Try to place a  Test.StartTest(); after this insert 
 
@isTest(SeeAllData=true)

public with sharing class XeroCalloutUtilityTest{
        
     Public Static TestMethod Void Testcallout(){
         
             Xero_Settings__c xeroseting = new Xero_Settings__c();
             xeroseting.name = 'testName';
             xeroseting.Consumer_Key__c = 'testConsumer';
             xeroseting.Endpoint__c = 'www.xero.com';
             insert xeroseting;
       Test.StartTest();
       SingleRequestMock fakeResponse = new SingleRequestMock(200, 'Complete', '[{"Name": "sForceTest1"}]', null);
       Test.setMock(HttpCalloutMock.class, fakeResponse);  
       XeroCalloutUtility.executeCallout(' request.setMethod','request.setEndpoint','request.setBody');
       
        HttpRequest request =  new HttpRequest();
        String contentType = request.getHeader('Content-Type');
        String actualValue = request.getBody();
        request.setHeader('Accept', 'application/json');
        //HttpResponse response = new HTTP().send(request);
        Test.StopTest();     
     }  
}

Hope this will help you .

Thanks
karthik



 
vikas rathi91vikas rathi91

Hello karthikeyan perumal 

Thanks 
My test class is passed 

I want know more when I am use seealldata=true then class is passed and coverage is 100% and when I am not use(SeeAllData=true) then class is fail and coverage is 57% 

How to solve this problem 


Thanks 
Vikas Rathi

karthikeyan perumalkarthikeyan perumal
Hello, 

What is the error message you are getting?

Thanks
karthik