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
Huy NguyenHuy Nguyen 

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

Hi expert, I have controller below but i got the error from the title. Could any one to take a look that issue ?

public class demo {
    
    private ApexPages.standardcontroller stdCtrl;
    private final String serviceEndpoint= 'http://md5.jsontest123.com/';
    public Contact contact;
    public boolean displayPopup {get; set;}     
    public String Response { get; set;}
    public String Headers { get; set; }
    public demo(ApexPages.standardController controller)
    {    
        stdCtrl = controller;
        this.contact = (Contact)controller.getRecord();
        contact  = [Select name,email from Contact where id=:contact.id limit 1];
        sendEmail();
    }
    
    public PageReference doDelete()
    { 
        PageReference ref = stdCtrl.delete(); 
        ref.getParameters().put('nooverride','1');
        return ref;
    }
    
    public boolean sendReq() 
    {
        //String conName = contact.Name;
        boolean validated= true;
        String ERR_503 = 'Service Unavailable. Can not access webservice at the moment';
        String errorMessage = '';
        Integer httpStatusCode = 0;
        String httpStatus = '';
        String resBody = '';
        
        // Get the XML document from the external server
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint(serviceEndpoint+'?text='+contact.Name);
        
        req.setMethod('GET');
        HttpResponse res = http.send(req);
        httpStatusCode = res.getStatusCode();
        httpStatus = res.getStatus();
        resBody = res.getBody();
        System.debug(httpStatusCode);
        System.debug(httpStatus);
        System.debug(resBody);
        if (httpStatusCode == 200)
        {
            validated= true;
            system.debug('validated'+validated);
            
            //ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.Info,'1'));
            //showPopup();
            
        }
        
        else if(httpStatusCode == 503)
        {
            validated= false;
            errorMessage +=ERR_503;
            throw new errorException(errorMessage);
        }
        return true;
    }
    
    public void closePopup() {        
        displayPopup = false;    
    }
    
    public void showPopup() {        
        displayPopup = true;    
    }
    public void sendEmail()
    {
        system.debug('sendReq'+sendReq());
        if(sendReq())
        {
            displayPopup = true; 
            List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();        
            String[] toAddress = new String[] {contact.Email};
            email.setToAddresses(toAddress);
            email.setSubject('Deleted Contact Alert');
            email.setPlainTextBody('This message is to alert you that the contact named ' + contact.Name + ' has been deleted.');
            emails.add(email);
            Messaging.sendEmail(emails);         
        }
        
    }   
}
test class :

@isTest(SeeAllData=false)
public class testdemoController {
    static Contact cont;    
      @isTest static void testNormalCase(){
        Init();
        System.Test.setMock(HttpCalloutMock.class, new ExampleCalloutMock());
        demo controller = new demo(new ApexPages.StandardController(cont));
        controller.doDelete();
    }
    static void Init()
    {
        System.Test.startTest();
        Account acct = new Account(Name='test');
        insert acct;
        cont = new Contact(Email='nlhuy@tma.com.vn',LastName='TestCont');
        insert cont;
        System.Test.stopTest();
    }
    
}
Huy NguyenHuy Nguyen
Sorry my bad I got the error System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out