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
Harjeet Singh 13Harjeet Singh 13 

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

Hi,

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;
        mDataList.add(mData);
        newCase.MetaData=mDataList;
        
        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>();
        podReq.inputHttpHeaders_x.put('Authorization',authorizationHeader);
        RetrievePODImageRequestEIL3.Case2 res = podReq.getPODImage(newCase);
        if(res.ErrorCode=='SUCCESS'){
            if(res.Attachment.size()>0){
                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;
                attList.add(attFile);
                }
                try{
                insert attList;
                }catch(Exception e){
                   system.debug('Exception-->'+e);
               }
                System.debug('Inserted Attachment');
            }
        }else
            System.debug('Error invoking Service');
    }
}

Below is my test class:
 
@isTest(SeeAllData=true)
Public class invokeEILService_Test{
    static testMethod void retrievePODMeth() {
        
    case c = UnitTestHelper.getCaseInstance();
    insert c;
        
    system.assert(c.Id!=null);
    invokeEILService invoke = new invokeEILService();
    invokeEILService.retrievePOD(c.Id);
    }
}

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

Kindly help me

Thanks & Regards,
Harjeet
DeveloperDeveloper
Hi Harjeet,

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

https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000EFozgUAD

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
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,
Harjeet
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.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm

 
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,
Harjeet
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,
Harjeet
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,
Harjeet