+ Start a Discussion
DnyaneshwarDnyaneshwar 

How can i increase code coverage of my class

Hi,
In my class i have a method: (Bold-Italic lines are not covered)
public PageReference sendBulkSMSToLeads(){
        //debug = leadStatus + ':' + intakeMonth + ':' + intakeYear + ':' + message;
        List<Lead> matchingLeadsList = [Select MobilePhone from Lead where Status =:leadStatus and Intake_Month__c =:intakeMonth
                                        and Intake_Year__c =:intakeYear];
        Set<String> uniquePhoneNumberList = new Set<String>();                                
        if(matchingLeadsList != null && matchingLeadsList.size() > 0){
            String commaSeparatedMobileNumbers = '';
            for(Lead lead : matchingLeadsList){
                
                if(lead.MobilePhone != null && lead.MobilePhone != ''){
                    if(!uniquePhoneNumberList.contains(lead.MobilePhone)){
                        uniquePhoneNumberList.add(lead.MobilePhone);
                        commaSeparatedMobileNumbers = commaSeparatedMobileNumbers + ',' + lead.MobilePhone;
                    }
                }
            }
            
            if(commaSeparatedMobileNumbers.length() > 0){
                commaSeparatedMobileNumbers = commaSeparatedMobileNumbers.substring(1, commaSeparatedMobileNumbers.length());
            }
            
            //debug = commaSeparatedMobileNumbers;
            
           
            
            String payLoad = 'username=global&password=muzztech&mobile=' +commaSeparatedMobileNumbers + '&sendername=SPJAIN&message='+message;
            Http h = new Http();
            HttpRequest req = new HttpRequest();
            req.setEndpoint('http://priority.muzztech.in/sms_api/sendsms.php'); 
            req.setMethod('POST');
            req.setBody(payLoad);
            HttpResponse res = h.send(req);
            
            ApexPages.Message myMsg = new  ApexPages.Message(ApexPages.Severity.INFO,'Bulk SMS initiated for ' + matchingLeadsList.size() + ' leads');
            ApexPages.addMessage(myMsg); 
            
        }else{
            ApexPages.Message myMsg = new  ApexPages.Message(ApexPages.Severity.ERROR,'No matching leads found to send SMS');
            ApexPages.addMessage(myMsg); 
        }
        return null;
    }
For which i have written this test class method:
@isTest 
      global class SMSControllerTest {
      global class MockHttpResponseGenerator implements HttpCalloutMock
        {
          global HTTPResponse respond(HTTPRequest req) {
            // Optionally, only send a mock response for a specific endpoint and method.
            System.assertEquals('http://priority.muzztech.in/sms_api/sendsms.php', req.getEndpoint());
            System.assertEquals('POST', req.getMethod());
            
            HttpResponse res = new HttpResponse();
            res.setHeader('Content-Type', 'application/json');
            res.setBody('{"username":"global","password":"muzztech","mobile":"7854945777","message":"test message"}');//add your fake JSON here
            res.setStatusCode(200);
            return res;
            } 
         }  
         
        static testMethod void sendBulkSMSToLeadsTest() {
          Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
          Test.startTest();
          SMSController ctrl = new SMSController();        
          ctrl.leadStatus = 'Hot';
          ctrl.intakeMonth = 'January';
          ctrl.intakeYear = '2017';
          ctrl.sendBulkSMSToLeads();
          //Assert the response here
          Test.stopTest();
        }
        
       
      }
How can i increase the code coverage of this method? Thanks a ton for suggestions!
 
Best Answer chosen by Dnyaneshwar
BALAJI CHBALAJI CH
Hi Dnyaneshwar,

@Hemant Soni 21, is absolutely correct. You have to cretae Test data in your Test class. 
Please find below modified Test Class:
@isTest 
global class SMSControllerTest {
    global class MockHttpResponseGenerator implements HttpCalloutMock
    {
        global HTTPResponse respond(HTTPRequest req) {
            // Optionally, only send a mock response for a specific endpoint and method.
            System.assertEquals('http://priority.muzztech.in/sms_api/sendsms.php', req.getEndpoint());
            System.assertEquals('POST', req.getMethod());
            
            HttpResponse res = new HttpResponse();
            res.setHeader('Content-Type', 'application/json');
            res.setBody('{"username":"global","password":"muzztech","mobile":"7854945777","message":"test message"}');//add your fake JSON here
            res.setStatusCode(200);
            return res;
        } 
    }  
    
    static testMethod void sendBulkSMSToLeadsTest() {
        
        Lead l = new Lead();
        l.LastName = 'LastName';
        l.Company = 'Test';
        l.Intake_Month__c = 'January';
        l.Intake_Year__c = '2017';
        l.Status = 'Hot';
        l.MobilePhone = '9999999999';
        insert l;
        
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
        Test.startTest();
        SMSController ctrl = new SMSController();        
        ctrl.leadStatus = 'Hot';
        ctrl.intakeMonth = 'January';
        ctrl.intakeYear = '2017';
        ctrl.sendBulkSMSToLeads();
        //Assert the response here
        Test.stopTest();
    }
}

Let us know if that helps you.

Best Regards,
BALAJI

All Answers

DnyaneshwarDnyaneshwar
PS: Line 7 -35 are not getting Covered
BALAJI CHBALAJI CH
Hi Dnyaneshwar,

@Hemant Soni 21, is absolutely correct. You have to cretae Test data in your Test class. 
Please find below modified Test Class:
@isTest 
global class SMSControllerTest {
    global class MockHttpResponseGenerator implements HttpCalloutMock
    {
        global HTTPResponse respond(HTTPRequest req) {
            // Optionally, only send a mock response for a specific endpoint and method.
            System.assertEquals('http://priority.muzztech.in/sms_api/sendsms.php', req.getEndpoint());
            System.assertEquals('POST', req.getMethod());
            
            HttpResponse res = new HttpResponse();
            res.setHeader('Content-Type', 'application/json');
            res.setBody('{"username":"global","password":"muzztech","mobile":"7854945777","message":"test message"}');//add your fake JSON here
            res.setStatusCode(200);
            return res;
        } 
    }  
    
    static testMethod void sendBulkSMSToLeadsTest() {
        
        Lead l = new Lead();
        l.LastName = 'LastName';
        l.Company = 'Test';
        l.Intake_Month__c = 'January';
        l.Intake_Year__c = '2017';
        l.Status = 'Hot';
        l.MobilePhone = '9999999999';
        insert l;
        
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
        Test.startTest();
        SMSController ctrl = new SMSController();        
        ctrl.leadStatus = 'Hot';
        ctrl.intakeMonth = 'January';
        ctrl.intakeYear = '2017';
        ctrl.sendBulkSMSToLeads();
        //Assert the response here
        Test.stopTest();
    }
}

Let us know if that helps you.

Best Regards,
BALAJI
This was selected as the best answer
DnyaneshwarDnyaneshwar
Thanks a lot for the concept and answer!!!