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
Daniel B ProbertDaniel B Probert 

wrapper test class help

hi all, i'm unable to get my test class above 63% for this wrapper.

 

here is the class

public class SMSSendBulk {
    
    public List<cContact> contactList {get; set;}
    public List<cContact> getContacts() {
        if(contactList == null) {
            contactList = new List<cContact>();
            for(Contact c: [select Id, Name, Email, country__r.Name, district__r.name,sms_enabled__c, mobilePhone from Contact where SMS_Enabled__c = true]) {
                contactList.add(new cContact(c));
            }
        }
        return contactList;
    }
   public SMSSendBulk() {
    }
   public SMS_Message__c smss;
    public SMSSendBulk(ApexPages.StandardController controller) {
        smss = (SMS_Message__c) controller.getRecord();
        system.debug(smss);
    }
   public PageReference processSelected() {
        
    
        List<Contact> selectedContacts = new List<Contact>();
        
        for(cContact cCon: getContacts()) {
            if(cCon.selected == true) {
                selectedContacts.add(cCon.con);
            }
        }
        for(Contact con: selectedContacts) {
            sms_message__c newSMS = new SMS_Message__c();
            newSMS.Contact__c = con.ID;
            newSMS.Message__c = smss.Message__c;
            newSMS.status__c = 'Sent';
            newSMS.direction__c = 'Outbound';
            newSMS.bulksms__c = true;
            newSMS.date_sent__c = system.Now();
            newSMS.recordtypeid = [select Id from RecordType where Name = 'Outbound' and SobjectType = 'SMS_Message__c'].Id;
            insert newSMS;  
        }
        contactList=null; // we need this line if we performed a write operation  because getContacts gets a fresh list now
        return null;
    }

    public class cContact {
        public Contact con {get; set;}
        public Boolean selected {get; set;}
    public cContact(Contact c) {
            con = c;
            selected = false;
        }
    }
}

 this is the vf incase it's needed or wanted:

 

<apex:page standardController="SMS_Message__c" extensions="SMSSendBulk">
<apex:form id="frmSubmitMassTask">         
        <apex:messages />
        <apex:pageBlock title="New Bulk SMS Service" mode="Edit" id="field_section">      
            <apex:pageBlockSection title="SMS Message" columns="1">
                <apex:inputField value="{!SMS_Message__c.Message__c}" styleClass="smsMessage" required="true" />  
                <p class="countdown"> Characters remaining <span id="count"> 160</span></p>
                <apex:pageBlockSectionItem />                                   
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Send SMS" action="{!processSelected}" rerender="table"/>
            </apex:pageBlockButtons>

            <!-- In our table we are displaying the cContact records -->
            <apex:pageBlockTable value="{!contacts}" var="c" id="table">
                <apex:column ><apex:facet name="header">
                        <apex:inputCheckbox onclick="checkAll(this,'checkedone')"/>
                </apex:facet>
                    <apex:inputCheckbox value="{!c.selected}" id="checkedone"/></apex:column>
                
                <!-- This is how we access the contact values within our cContact container/wrapper -->
                <apex:column value="{!c.con.Name}" />
                <apex:column value="{!c.con.MobilePhone}" />
                <apex:column value="{!c.con.Country__r.name}" />
                <apex:column value="{!c.con.District__r.name}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"/>
    <script>
        var j$ = jQuery.noConflict();
        
        j$(document).ready(function(){
            updateRemaining();
            j$(".smsMessage").bind('input keyup paste', function(){ 
                updateRemaining();
            });
        });
       
        function updateRemaining(){
            var count = j$("#count")
            var length = j$(".smsMessage").val().length;
            var remaining = 160 - length;
            count.text(remaining);
            
            if(remaining < 0){
                count.addClass('red');
            }else{
                count.removeClass('red');
            }
        }
    </script>
    <script type="text/javascript">
    function checkAll(cb,cbid)
        {
            var inputElem = document.getElementsByTagName("input");                     
            for(var i=0; i<inputElem.length; i++)
            {             
                 if(inputElem[i].id.indexOf(cbid)!=-1){                                        
                    inputElem[i].checked = cb.checked;
                }
            }
        }
</script>
    <style>
        .smsMessage{
            width: 135px;
            height: 100px;
        }
        .countdown{
        margin-left: 18%;
        margin-top: -4px;
        }
        .red{
            color: #CC0000;
            font-weight: bold;
        }
    
    </style>
</apex:page>

 this is my test class so far:

 

@isTest
public class Test_SMSSendBulk {
    static testMethod void myUnitTest() {     
        
        Country__c count=new country__c(Name='test');
        insert count;
        district__c dist=new district__c(Name='test',country__c=count.id);
        insert dist;
        contact con=new contact(lastname='test',mobilephone='+44717417554',district__c=dist.id,sms_enabled__c=true);
        insert con;
        List<Contact> contactlist = [select LastName from contact limit 1];
        // Instantiate the controller
        PageReference pageRef = Page.SendBulkSMS;
        Test.setCurrentPage(pageRef);
               
        SMSSendBulk sc = new SMSSendBulk();      
        Pagereference S = sc.processSelected();
 
        sc.contactList=sc.getContacts();  
        sc.contactList[0].selected=true;
             
        }
}

 the last line in here:

 

sc.contactList[0].selected=true; doesn't seem to be selecting the contact on the page and so it's not showing as having tested that line, also when i add in:

 

sc.smss.message__c = 'test';

as an attempt to include the message i want sending i get a null reference error.

 

any guidance the selected=true is driving me crazy if i change it to [1] i get an error(list out of bounds).

 

cheers

dan

Daniel B ProbertDaniel B Probert

ok so i've figured out how to get my coverage to 96% but just one more line left to test any ideas.

 

new text code:

 

@isTest
public class Test_SMSSendBulk {
    static testMethod void myUnitTest() {     
        
        Country__c count=new country__c(Name='test');
        insert count;
        district__c dist=new district__c(Name='test',country__c=count.id);
        insert dist;
        contact con=new contact(lastname='test',mobilephone='+44717417554',district__c=dist.id,sms_enabled__c=true);
        insert con;
        sms_message__c smsnew= new SMS_message__c(Contact__c=con.id,message__c='test');
        insert smsnew;
        
        string contactIndex=con.ID;

        // Instantiate the controller
        PageReference pageRef = Page.SendBulkSMS;
        Test.setCurrentPageReference(pageRef);
      
        ApexPages.StandardController controller = new ApexPages.StandardController(smsnew);
        SMSSendBulk sc = new SMSSendBulk(controller);  
        
        sc.contactList=sc.getContacts(); 
        sc.contactList[0].selected=true;
        sc.smss.message__c = 'test';
        Pagereference S = sc.processSelected();
        
             
        }
}

 still to test:

 

public SMSSendBulk() {
    }

 

cheers dan

Daniel B ProbertDaniel B Probert
ignore i've removed the public smssendbulk(){
}
it was a duplicate entry and not needed :) now i've got 100%...