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
Subbeh2Subbeh2 

Average test coverage across all Apex Classes and Triggers is not enough

Hi,

 

I'm struggling to get the following code deployed. It keeps coming with the message "Average test coverage across all Apex Classes and Triggers is 24%, at least 75% test coverage is required"

 

Please give me some help on what to include in the test method.

 

 

public class callOut {
    public static HttpRequest buildWebServiceRequest(String pid, String gid, String pass, String getLead, String password){
        // Build the XML Request
        string body = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:reg="http://test">' +
                          '<soapenv:Header/>' +
                          '<soapenv:Body>' +
                              '<reg:RegisterUserWithAutoId_test>' +
                                  '<reg:partnerID>'+pid+'</reg:partnerID>' +
                                  '<reg:partnerGuid>'+gid+'</reg:partnerGuid>' +
                                  '<reg:partnerPassword>'+pass+'</reg:partnerPassword>' +
                                  '<reg:clientName>'+getLead+'</reg:clientName>' +
                                  '<reg:clientPassword>'+password+'</reg:clientPassword>' +
                              '</reg:RegisterUserWithAutoId_test>' +
                          '</soapenv:Body>' +
                      '</soapenv:Envelope>';
        system.debug(body);

        //construct an HTTP request
        HttpRequest req = new HttpRequest();
        req.setHeader('content-type', 'text/xml; charset=utf-8');
        req.setHeader('Host','test');
        req.setEndpoint('http://test');
        req.setMethod('POST');
        req.setBody(body); 
        return req;
    }
    
    public static HttpResponse invokeWebService(Http h, HttpRequest req){
        //Invoke Web Service
        HttpResponse res = h.send(req);
        return res;
    }
    
    public static void handleWebServiceResponse(HttpResponse res, String password, String id){
        //Parse and apply logic to the res message
        if (res.getStatusCode() == 200) {
    
            string xml = res.getBody();
            string xmlRegex = '^.*clientid="';
            pattern xmlPattern = pattern.compile(xmlRegex);
            matcher xmlMatcher = xmlPattern.matcher(xml);
            List<String> result = xml.split(xmlRegex);
            List<String> result2 = result[1].split('\"');
            
            Lead l = new Lead(Id=id);
            List<Lead> getLead = [select name,email,LeadSource from lead where id = :id];
            
            l.ClientPassword__c = password;
            l.ClientID__c = result2[0];
            update l;
      
            //send email
            SendMail.newLead(getLead[0].id, getLead[0].email);
      
            System.debug('Callout body: ' + res.getBody());
        } else {
            System.debug('Callout failed: ' + res);
        } 
    }
    
    @Future(callout=true)
    public static void main(Id id){
    
        //apply business logic
        String pid = '1234';
        String gid = '1234';
        String pass = 'test';
        
        Lead l = new Lead(Id=id);
        List<Lead> getLead = [select name,email,LeadSource from lead where id = :id];
        
        if (getLead[0].LeadSource == 'Web2Lead') {
            string password = EncodingUtil.convertToHex(Crypto.generateDigest('MD5',Blob.valueOf(getLead[0].name + Crypto.getRandomInteger()))).substring(0, 8);
        
            //now need to make web service callout

            //build the http request
            Http h = new Http();
            HttpRequest req = buildWebServiceRequest(pid, gid, pass, getLead[0].name, password);
   
            //invoke web service call 
            HttpResponse res = invokeWebService(h, req);
   
            //handling the response
            handleWebServiceResponse(res, password, id);    
        }
    }
    
    static testMethod void testWebService(){
        //build the http request
        HttpRequest req = buildWebServiceRequest('test', 'test', 'test', 'test', 'test');

        //write apex code to build a sample HttpResponse object
        HttpResponse res = new HttpResponse();

        //Apply test data and attributes to the HttpResponse object as needed
        handleWebServiceResponse(res, 'test', 'test');
    }
}