+ Start a Discussion
Harjeet Singh 13Harjeet Singh 13 

Test class error: Methods defined as TestMethod do not support Web service callouts


I am trying to write a test class. My current code coverage is 64%(22 lines covered out of 32 lines).
But I am getting error while running test class as:
Methods defined as TestMethod do not support Web service callouts

Below is my class:
global with sharing class invokeEILService {
    webservice static void retrievePOD(string RowId){
        Case caseRec = [Select Id, Con_Note__r.Name, Con_Note__r.Due_Delivery_Date__c from Case WHERE Id=:RowId];
        RetrievePODImageRequestEIL3.Case_x newCase= new RetrievePODImageRequestEIL3.Case_x();
        RetrievePODImageRequestEIL3.MetaData[] mDataList = new List<RetrievePODImageRequestEIL3.MetaData>();
        RetrievePODImageRequestEIL3.MetaData mData = new RetrievePODImageRequestEIL3.MetaData();
        List<Attachment> attList = new List<Attachment>();
        mData.Key = 'deliveryDate';
        mData.Value = string.valueof(caseRec.Con_Note__r.Due_Delivery_Date__c);
        newCase.ID= RowId;
        newCase.ImageType = 'pod';
        newCase.BusinessUnit = 'ipec';
        newCase.ConNote = caseRec.Con_Note__r.Name;
        String username= 'salesforce_uat';
        String password= 'salesforce_uat';
        Blob headerValue = Blob.valueOf(username + ':' + password);
        String authorizationHeader = 'Basic '+ EncodingUtil.base64Encode(headerValue);
        RetrievePODImageRequestEIL3.aSalesforce_ws_provider_PODRequest_receivePODImageRequest_Port podReq = new RetrievePODImageRequestEIL3.aSalesforce_ws_provider_PODRequest_receivePODImageRequest_Port();
        podReq.inputHttpHeaders_x = new Map<String, String>();
        RetrievePODImageRequestEIL3.Case2 res = podReq.getPODImage(newCase);
                for(integer i=0; i<res.Attachment.size();i++){
                Attachment attFile = new Attachment();
                attFile.ParentId = RowId;
                attFile.Body = EncodingUtil.base64Decode(res.Attachment[i].Body);
                attFile.Name = res.Attachment[i].Name;
                attFile.ContentType = res.Attachment[i].ContentType;
                insert attList;
                }catch(Exception e){
                System.debug('Inserted Attachment');
            System.debug('Error invoking Service');

Below is my test class:
Public class invokeEILService_Test{
    static testMethod void retrievePODMeth() {
    case c = UnitTestHelper.getCaseInstance();
    insert c;
    invokeEILService invoke = new invokeEILService();

I did googling also but didn't get what should do for getting my test class pass.

Kindly help me

Thanks & Regards,
Hi Harjeet,

May I request you to please check for Test Class Generator App from APP Exchange.Please refer the below link.


I hope it will be helpful.

Please mark it as best answer if the information is informative.

Thanks & Regards 
Gopal M.

Harjeet Singh 13Harjeet Singh 13
Hi Pavit,

Thanks for your response. I already saw these blogs and salesforce links but couldn't get the extract from these.

Kindly help me

Thanks & Regards,
Harjeet Singh 13Harjeet Singh 13
Hi Gopal,

Thanks for your response. I know about test class generator app and already installed but not able to cover a single line of code using the same.

What I wrote is atleast covering 64% code coverage but getting an error while running class.

Kindly help me

Thanks & Regards,
Dushyant SonwarDushyant Sonwar
Harjeet ,
You can do two things for this.
1)Either avoid covering that part that do webservice Callout using Test.isRunningTest.
2)Create HttpCalloutMock Interface in your org to cover Web service callouts code.

Harjeet Singh 13Harjeet Singh 13
Thanks Dushyant for your response.

I already saw one which you have shared with me.

Kindly help me

Thanks & Regards,
Dushyant SonwarDushyant Sonwar
no one can help you for this , you need to create your own mock interface and try implementing in your org , rather than asking for whole code written for you.

People will guide you in forum but hardly they will write you full code for you and also this will not help you to learn.

First try implementing this one and then ask in forums if you get stuck during implementation. 
Harjeet Singh 13Harjeet Singh 13
Hi Dushyant,

I am aware about mock interface and already implemented mock interface for so many classes in the past. But this time I am not getting the coverage

Kindly help me

Thanks & Regards,
Dushyant SonwarDushyant Sonwar
post your httpmockinterface code that you have implemented in your org for your testclass.
Harjeet Singh 13Harjeet Singh 13
Hi Dushyant,

I have code coverage for class

Thansk & Regards,