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
Intigration vIntigration v 

Test Class Error:Methods defined as TestMethod do not support Web service callouts

Hi,
Can some one please help me on this test class error.I have used the Apex class in my java script button to display error message based on the return boolean value.
Javascript button code:

{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
var isDocument=sforce.apex.execute("checkDoconButton", "checkDocVersion", {LeadId:"{!Lead.Id}"});
if(isDocument=="true"){
alert('document already exists');
}
Apex class:
global without sharing class checkDoconButton {
   webservice static Boolean checkDocVersion(id LeadId){
        list<ContentDocumentLink>Doclst= new list<ContentDocumentLink>();
        list<string>verifiedDoc= new list<string>();
        list<Lead> lstLead=new list<Lead>();
        list<string> lstdoctitle=new list<string>();
        Boolean isDocument;
        lstLead=[select Phone,City__c from Lead where id=:LeadId];
        string city='Ver.'+' 0'+lstLead[0].City__c;
        Doclst= [SELECT ContentDocumentId,contentdocument.title FROM ContentDocumentLink WHERE LinkedEntityId =:LeadId];
        
        for(ContentDocumentLink doclink:Doclst){
            if(doclink.contentdocument.title.contains(city)){
                verifiedDoc.add(doclink.contentdocument.title);
            }
            
        }
        if(verifiedDoc.size()>0){
        isDocument=true;
        }
        else{
            isDocument=false;
        }
        return isDocument;
    }

}
Test Class:
@istest
public class checkDoconButtonTest {
   @istest
static void  checkdocbutton() {
    Test.startTest();
    Lead l = new Lead();
    l.LastName='sample';
    l.Company='sample';
    l.Status='Qualified';
    l.City__c='hyderabad';
    insert l;
    ContentVersion cv = new ContentVersion();
cv.Title = 'Ver. 0hyderabad';
cv.PathOnClient = 'TesthyderabadDocument.pdf';
cv.VersionData = Blob.valueOf('Test Content');
cv.IsMajorVersion = true;
Insert cv;

//Get Content Documents
Id conDocId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;

//Create ContentDocumentLink 
ContentDocumentLink cdl = New ContentDocumentLink();
cdl.LinkedEntityId = l.Id;
cdl.ContentDocumentId = conDocId;
cdl.shareType = 'V';
Insert cdl;
    
   Boolean check= checkDoconButton.checkDocVersion(l.id);
    system.assertEquals(True, check);
    Test.stopTest();
}
}

Getting the error :Methods defined as TestMethod do not support Web service callouts
can anyone please tell me how can i pass this test class?
Thank you!
SwethaSwetha (Salesforce Developers) 
HI ,
You need to call Test.setMock(...) in your test class once you've implemented the required interfaces to prevent this particular error message.

You also need to make sure you set the mock within the context. If you call Test.startTest() in between setting your mock and making the callout, it won't work.

https://salesforce.stackexchange.com/questions/159285/methods-defined-as-testmethod-do-not-support-web-service-callouts-error-when-t

https://salesforce.stackexchange.com/questions/330160/error-testmethod-do-not-support-web-service-callouts-in-testdatafactory-test-met

https://salesforce.stackexchange.com/questions/31808/method-defined-as-testmethod-do-not-support-web-service-callouts-test-skipped

If this information helps, please mark the answer as best. Thank you