1

I had tried to cove a apex class with my test class. I am able to get 98% code coverage,except thet the test class is failing due to this error:

Methods defined as TestMethod do not support Web service callouts, test skipped 

How can we over come this?

apex class:

public class TimelineRiskAuditPDFStub {
    public class LogInfo {
        public String category;
        public String level;
        private String[] category_type_info = new String[]{'category','http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF',null,'1','1','false'};
        private String[] level_type_info = new String[]{'level','http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF','true','false'};
        private String[] field_order_type_info = new String[]{'category','level'};
    }
    public class AllowFieldTruncationHeader_element {
        public Boolean allowFieldTruncation;
        private String[] allowFieldTruncation_type_info = new String[]{'allowFieldTruncation','http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF','true','false'};
        private String[] field_order_type_info = new String[]{'allowFieldTruncation'};
    }
    public class DebuggingHeader_element {
        public TimelineRiskAuditPDFStub.LogInfo[] categories;
        public String debugLevel;
        private String[] categories_type_info = new String[]{'categories','http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF',null,'0','-1','false'};
        private String[] debugLevel_type_info = new String[]{'debugLevel','http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF','true','false'};
        private String[] field_order_type_info = new String[]{'categories','debugLevel'};
    }
    public class CallOptions_element {
        public String client;
        private String[] client_type_info = new String[]{'client','http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF','true','false'};
        private String[] field_order_type_info = new String[]{'client'};
    }
    public class addPDFResponse_element {
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF','true','false'};
        private String[] field_order_type_info = new String[]{};
    }
    public class SessionHeader_element {
        public String sessionId;
        private String[] sessionId_type_info = new String[]{'sessionId','http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF','true','false'};
        private String[] field_order_type_info = new String[]{'sessionId'};
    }
    public class DebuggingInfo_element {
        public String debugLog;
        private String[] debugLog_type_info = new String[]{'debugLog','http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF','true','false'};
        private String[] field_order_type_info = new String[]{'debugLog'};
    }
    public class addPDF_element {
        public String[] caseIds;
        public String pdfName;
        private String[] caseIds_type_info = new String[]{'caseIds','http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF',null,'0','-1','true'};
        private String[] pdfName_type_info = new String[]{'pdfName','http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF',null,'1','1','true'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF','true','false'};
        private String[] field_order_type_info = new String[]{'caseIds','pdfName'};
    }
    public class TimelineRiskAuditPDF {
        public String endpoint_x = label.TimelinePDFWSDLendPoint;
        public Map<String,String> inputHttpHeaders_x;
        public Map<String,String> outputHttpHeaders_x;
        public String clientCertName_x;
        public String clientCert_x;
        public String clientCertPasswd_x;
        public Integer timeout_x;
        public TimelineRiskAuditPDFStub.CallOptions_element CallOptions;
        public TimelineRiskAuditPDFStub.SessionHeader_element SessionHeader;
        public TimelineRiskAuditPDFStub.DebuggingInfo_element DebuggingInfo;
        public TimelineRiskAuditPDFStub.AllowFieldTruncationHeader_element AllowFieldTruncationHeader;
        public TimelineRiskAuditPDFStub.DebuggingHeader_element DebuggingHeader;
        private String CallOptions_hns = 'CallOptions=http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF';
        private String SessionHeader_hns = 'SessionHeader=http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF';
        private String DebuggingInfo_hns = 'DebuggingInfo=http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF';
        private String AllowFieldTruncationHeader_hns = 'AllowFieldTruncationHeader=http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF';
        private String DebuggingHeader_hns = 'DebuggingHeader=http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF';
        private String[] ns_map_type_info = new String[]{'http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF', 'TimelineRiskAuditPDFStub'};
        public void addPDF(String[] caseIds,String pdfName) {
            TimelineRiskAuditPDFStub.addPDF_element request_x = new TimelineRiskAuditPDFStub.addPDF_element();
            request_x.caseIds = caseIds;
            request_x.pdfName = pdfName;
            TimelineRiskAuditPDFStub.addPDFResponse_element response_x;
            Map<String, TimelineRiskAuditPDFStub.addPDFResponse_element> response_map_x = new Map<String, TimelineRiskAuditPDFStub.addPDFResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF',
              'addPDF',
              'http://soap.sforce.com/schemas/class/TimelineRiskAuditPDF',
              'addPDFResponse',
              'TimelineRiskAuditPDFStub.addPDFResponse_element'}
            );
            response_x = response_map_x.get('response_x');
        }
    }
}

Test Class:

@IsTest
private with sharing class TimelineRiskAuditPDFStubTest
{
        public String[] caseIds;
         public String pdfName;
    private static testMethod void coverTypes()
    {

        new TimelineRiskAuditPDFStub.LogInfo();
        new TimelineRiskAuditPDFStub.AllowFieldTruncationHeader_element();
        new TimelineRiskAuditPDFStub.DebuggingHeader_element();
        new TimelineRiskAuditPDFStub.CallOptions_element();
        new TimelineRiskAuditPDFStub.addPDFResponse_element();
        new TimelineRiskAuditPDFStub.SessionHeader_element();
        new TimelineRiskAuditPDFStub.DebuggingInfo_element();
            new TimelineRiskAuditPDFStub.addPDF_element();
                new TimelineRiskAuditPDFStub.TimelineRiskAuditPDF();
                    // new TimelineRiskAuditPDFStub.addPDF(caseIds,pdfName);
                        //new TimelineRiskAuditPDFStub.DebuggingInfo_element();
        TimelineRiskAuditPDFStub.TimelineRiskAuditPDF abc = new TimelineRiskAuditPDFStub.TimelineRiskAuditPDF();
        String [] myList = new List<String>();
        myList.add('test');


        abc.addPDF(myList,'test');  
    }
}
  • 1
    post your code.. – Anu Aug 13 '15 at 10:54
3

See Testing Apex Callouts using HttpCalloutMock or Testing Web Service Callouts.

You can invoke the web service calls from your test providing you write code that is executed in place of the HTTP callout. That code is hooked into your test using the Test.setMock method.

Here is an outline for the HttpCalloutMock mock case:

@isTest
private class ExampleTest {

    private class RestMock implements HttpCalloutMock {
        Id sobId;
        RestMock(Id claimId) {
            this.claimId = sobId;
        }
        public HTTPResponse respond(HTTPRequest req) {
            // Can assert that the request is correct here
            // Generate a typical response
            String fullJson = ...
            HTTPResponse res = new HTTPResponse();
            res.setHeader('Content-Type', 'text/json');
            res.setBody(fullJson);
            res.setStatusCode(200);
            return res;
        }
    }

    @isTest
    static void test() {
        // Setup
        Test.setMock(HttpCalloutMock.class, new RestMock(sobId));
        Test.startTest();
        // Invoke web service
        Test.stopTest();
        // Assertions
    }
}
|improve this answer|||||
  • i have posted my code. can you give a small idea how it should be modified – Azeem Aug 13 '15 at 11:00
  • @Azeem It's the Testing Apex Callouts using HttpCalloutMock that is relevant to you. Start from there by writing your own class that implements HttpCalloutMock using the classes nested in TimelineRiskAuditPDFStub. – Keith C Aug 13 '15 at 11:09

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.