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
NasirNasir 

test method not able to cover IF statement

Hi

 

I am trying to write the test method for the class below.but i am not abloe to cover "if" statements.

//    Description: This is the controller for checking delivery of
//                 both Single SMS as well as Bulk SMS to Leads/Contacts.
//    Author: Abhilash K

public class DeliveryController {

private String mtId;
private String[] mtArray;
private Integer iter;
private String myId;
private String response;
private String storeResponse;
private SMSHttpRequestResponse shrr;
private String smsType;
private SMS__c smsObj;
private Bulk_SMS__c bulkObj;
private PageReference pr;
public String credit;

    public PageReference checkDelivery() {
        ApexPages.Message myPageErrMsg;
        storeResponse = '';
        shrr = new SMSHttpRequestResponse();
        try {
            mtId = ApexPages.currentPage().getParameters().get('mtid');
            myId = ApexPages.currentPage().getParameters().get('id');
            smsType = ApexPages.currentPage().getParameters().get('type');
            System.debug('### '+mtId+' '+myId+' '+smsType);
            if(smsType.equals('bulk')) {
                mtArray = mtId.split(',');
                iter = mtArray.size();
                System.debug('### mtArray: ' + mtArray); 
                System.debug('### iter: '+ iter);
            }
            else {
                mtArray = new String[] {mtId};
                iter = 1;
            }
            for(Integer i=0; i<iter; i++) {
                System.debug('### Inside for loop:' + i); 
                response = shrr.getContent('http://www.onewaysms.com.my/bulktrx.aspx?mtid='+ mtArray[i]);
            
                if(response.equals('0')) {
                    myPageErrMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Message Delivered');
                    storeResponse = storeResponse  + 'Delivered';
                }
                else if(response.equals('100')) {
                    myPageErrMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM,'Waiting');
                    storeResponse = 'Waiting';
                }
                else if(response.equals('-100')) {
                    myPageErrMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Invalid ID/not found');
                    storeResponse = 'Invalid ID';
                }
                else if(response.equals('-200')) {
                    myPageErrMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Sending Failed');
                    storeResponse = 'Sending Failed';
                }
                else {
                    myPageErrMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Checking Failed');
                    //storeResponse = 'Checking Failed';
                }
            }
            
            // Check SMS Credit balance
            credit = shrr.getContent('http://www.onewaysms.com.my/bulkcredit.aspx?apiusername=APIU6QF39AOLY&apipassword=APIU6QF39AOLY8UXUB');
            try {
                Integer x = Integer.valueOf(credit);
            }
            catch(Exception e) {
                credit = 'Checking Failed';
            }
            
            if(smsType.equals('single')) {
                smsObj = [select Id,Message_Status__c from SMS__c where Id=:myId][0];
                if(storeResponse != '')
                    smsObj.Message_Status__c = storeResponse;
                update smsObj;
            }
            else if(smsType.equals('bulk')) {
                bulkObj = [select Id,Message_Status__c from Bulk_SMS__c where Id=:myId][0];
                if(storeResponse != null)
                    bulkObj.Message_Status__c = storeResponse;
                update bulkObj;
            }        
        }
        catch(Exception e) {
           myPageErrMsg = new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage());
        }
        ApexPages.addMessage(myPageErrMsg);    
        return null;
    }
    
     public PageReference goBack() {
         pr = new PageReference('/' + myId);
         pr.setRedirect(true);
         return pr;
    }

    public String getCredit() {
        return credit;
    }
    public static testMethod void redirect(){
     	SMS__c smsObj =new SMS__c();
     	Bulk_SMS__c bul = new Bulk_SMS__c();
     	
     	DeliveryController delCon = new DeliveryController();
     	delCon.checkDelivery();
     	
     	delCon.getCredit();
     	delCon.goBack();
     	 
     	
     	
     }
}

 

Please help

 

forecast_is_cloudyforecast_is_cloudy

Looks like you need to write test code for an external Apex callout scenario. Please look at the 'Test Methods and Apex Callouts' section of this article - http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods- for tips on how to test Apex callouts.

NasirNasir
how to execute this line with "if " statements if(smsType.equals('bulk')) and all the if statements after that... Please suggest. Thanks