+ Start a Discussion
BaguiarBaguiar 

SOQL criteria not letting me buid a test method

Hi there,

 

Posted before here an issue I'm having with a test method and, after looking at it I've noticed the List index out of bounds: 0. Thats because of a criteria on my SOQL statement that reads : 

 

(c.GW_Volunteers__Volunteer_Availability__c INCLUDES (:stravail[0].Shift_weekday__c)) 

 

Well, the Shift_weekday__c is on another SOQL that pulls the record ID from the current page that the user is in. The controller (wrapperclasscontroller) was built so that the user hits a button from a page and it pulls a report based on the Shift_weekday__c from the current record on the page the user is hitting the button from. The report comes up and gives the user check boxes so that you can chose the records and hit a button that will create records on a diferent object (Not the CONTACT) using the current contact IDs.

 

MY issue is How do I call the page and set some test Shift_weekday__c from the test method, just so that when I call the getcontacts() , my List does not return 0 (zero) records. The test method is really where I'm stuck.

 

Here is the controller:

 

public class wrapperClassController {
        
    GW_Volunteers__Volunteer_Shift__c[] stravail = [Select id, GW_Volunteers__Volunteer_Job__c, Shift_weekday__c from GW_Volunteers__Volunteer_Shift__c where id = :System.currentPagereference().getParameters().get('id')];
     
    public List<cContact> contactList {get; set;}

    
    public List<cContact> getContacts() {
        if(contactList == null) {
            contactList = new List<cContact>();
            for(Contact c : [Select c.ID, c.GW_Volunteers__Volunteer_Availability__c, c.GW_Volunteers__Volunteer_Status__c, c.Name, c.Volunteers_OK__c FROM Contact c WHERE c.Volunteers_OK__c = True and (c.GW_Volunteers__Volunteer_Availability__c INCLUDES (:stravail[0].Shift_weekday__c)) ]) {
                
                contactList.add(new cContact(c));
            }
        }
        return contactList;
    }


    public PageReference processSelected() {

                
        List<Contact> selectedContacts = new List<Contact>();

        for(cContact cCon : getContacts()) {
            if(cCon.selected == true) {
                selectedContacts.add(cCon.con);
            }
        }

       
        System.debug('These are the selected Contacts...');
        for(Contact con : selectedContacts) {
            GW_Volunteers__Volunteer_Hours__c newvolu = new GW_Volunteers__Volunteer_Hours__c( GW_Volunteers__Contact__c = Con.id, GW_Volunteers__Volunteer_Job__c = stravail[0].GW_Volunteers__Volunteer_Job__c, GW_Volunteers__Volunteer_Shift__c = stravail[0].id, GW_Volunteers__Start_Date__c = system.today(), GW_Volunteers__Status__c = 'Scheduled', GW_Volunteers__Number_of_Volunteers__c = 1 );
            insert newvolu;
        
            system.debug(con);
        }
        return null;
    }


    public class cContact {
        public Contact con {get; set;}
        public Boolean selected {get; set;}

        public cContact(Contact c) {
            con = c;
            selected = false;
        }
    }
}

 The VF Page:

<apex:page controller="wrapperClassController" showHeader="false">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Add Volunteers to shift" action="{!processSelected}" rerender="table" onclick="window.close()"/>
            </apex:pageBlockButtons>
            <br><b>
            Please check the Volunteers to be added to the Shift selected and press the "Add Volunteers to Shift" button.
            </b></br><br></br>
            <!-- In our table we are displaying the cContact records -->
            <apex:pageBlockTable value="{!contacts}" var="c" id="table">
                <apex:column >
                    <!-- This is our selected Boolean property in our wrapper class -->
                    <apex:inputCheckbox value="{!c.selected}"/>
                </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.GW_Volunteers__Volunteer_Availability__c}" />
                <apex:column value="{!c.con.GW_Volunteers__Volunteer_Status__c}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 And Test Method (as far as I could go):

@isTest
private  class TestwrapperClassController
{
   static testMethod void testgetContacts()
   {
      // insert a test account
      Account acc=new Account(Name='Test123');
      Database.saveresult sr = database.insert(acc);
     
      // insert a test contact
      List<Contact> con=new List<Contact>{new Contact(FirstName='Lester', Lastname='DbigTester', accountID=acc.id, email='123123123@888555.com', Volunteers_OK__c  = True, GW_Volunteers__Volunteer_Availability__c = 'Wednesday morning' ), 
            new Contact(FirstName='Lester2', Lastname='DbigTester2', accountID=acc.id, email='1231@888555.com', Volunteers_OK__c  = True, GW_Volunteers__Volunteer_Availability__c = 'Friday morning')};
      insert con;

      PageReference pageRef = Page.Match_Availability;
       Test.setCurrentPageReference(pageRef);
       
      wrapperClassController stdCon=new wrapperClassController();
      system.debug('******TEST-stdCon='+ stdCon); 
           
      stdCon.contactList = stdCon.getcontacts();
      for (Integer i = 0; i < 3; i++) {
        system.debug('******TEST-getContacts[' + i + '].con=' + stdCon.ContactList[i].con);
    }
    System.assertEquals(true,stdCon.ContactList[1].con.Volunteers_OK__c);
    
      stdCon.ContactList[1].selected=True;  
           system.debug('******TEST-Contact 1 SELECT stdCon.ContactList[1].selected=' + 
        stdCon.ContactList[1].selected); 
        
         stdCon.processSelected();  
   
   }
}

 Thanks in advance and I really appreciatte any help! :)

B

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Could you please provide me current copy of your class and and testclass for which test coverage is 30% only. It will be helpful to tell then how to to improve code coverage.

All Answers

Shashikant SharmaShashikant Sharma

Make two changes

 

1) Create new instance of this Object GW_Volunteers__Volunteer_Shift__c at the start of your test method. and insert it.

 

    GW_Volunteers__Volunteer_Shift__c  GMVShift = new GW_Volunteers__Volunteer_Shift__c ();

    //Fill all req fields

   insert GMVShift ;

2) Change this part 

 

PageReference pageRef = Page.Match_Availability; 

     Test.setCurrentPageReference(pageRef);

 

to

 

PageReference pageRef = Page.Match_Availability;       

pageRef.getParameters(0.put('id' , GMVShift .id);

Test.setCurrentPageReference(pageRef);

 

 

Following these two steps should solve your problem

BaguiarBaguiar

Thanks Shashikant. And sorry for the delay on this!

I did what you said, raised my coverage to 30% but still having an issue in testing the Pagereference "processSelected()". Actually, as I run the test, it is not covering anything bellow "retrn contactList;" on the original class. attached is the image with the coverage. Image link below as well.

 

 

http://www.screencast.com/t/lcclVZS5f7

 

Thanks again for the help!

Shashikant SharmaShashikant Sharma

Could you please provide me current copy of your class and and testclass for which test coverage is 30% only. It will be helpful to tell then how to to improve code coverage.

This was selected as the best answer
BaguiarBaguiar

Sure!

Here is the class:

 

public class wrapperClassController {
        
    GW_Volunteers__Volunteer_Shift__c[] stravail = [Select id, GW_Volunteers__Volunteer_Job__c, Shift_weekday__c from GW_Volunteers__Volunteer_Shift__c where id = :System.currentPagereference().getParameters().get('id')];
     
    public List<cContact> contactList {get; set;}

    public List<cContact> getContacts() {
        if(contactList == null) {
            contactList = new List<cContact>();
            for(Contact c : [Select c.ID, c.GW_Volunteers__Volunteer_Availability__c, c.GW_Volunteers__Volunteer_Status__c, c.Name, c.Volunteers_OK__c FROM Contact c WHERE c.Volunteers_OK__c = True and (c.GW_Volunteers__Volunteer_Availability__c INCLUDES (:stravail[0].Shift_weekday__c)) ]) {
                // As each contact is processed we create a new cContact object and add it to the contactList
                contactList.add(new cContact(c));
            }
        }
        return contactList;
    }

    public PageReference processSelected() {

                
        List<Contact> selectedContacts = new List<Contact>();

        for(cContact cCon : getContacts()) {
            if(cCon.selected == true) {
                selectedContacts.add(cCon.con);
            }
        }

        System.debug('These are the selected Contacts...');
        for(Contact con : selectedContacts) {
            GW_Volunteers__Volunteer_Hours__c newvolu = new GW_Volunteers__Volunteer_Hours__c( GW_Volunteers__Contact__c = Con.id, GW_Volunteers__Volunteer_Job__c = stravail[0].GW_Volunteers__Volunteer_Job__c, GW_Volunteers__Volunteer_Shift__c = stravail[0].id, GW_Volunteers__Start_Date__c = system.today(), GW_Volunteers__Status__c = 'Scheduled', GW_Volunteers__Number_of_Volunteers__c = 1 );
            insert newvolu;
        
            system.debug(con);
        }
        return null;
    }

    public class cContact {
        public Contact con {get; set;}
        public Boolean selected {get; set;}

        
        public cContact(Contact c) {
            con = c;
            selected = false;
        }
    }
}

 And the test class (up to 30%):

 

@isTest
private  class TestwrapperClassController
{
   static testMethod void testgetContacts()
   {
      // insert a test account
      Account acc=new Account(Name='Test123');
      Database.saveresult sr = database.insert(acc);
     
      // insert a test contact
      List<Contact> con=new List<Contact>{new Contact(FirstName='Lester', Lastname='DbigTester', accountID=acc.id, email='123123123@888555.com', Volunteers_OK__c  = True, GW_Volunteers__Volunteer_Availability__c = 'Wednesday morning' ), 
            new Contact(FirstName='Lester2', Lastname='DbigTester2', accountID=acc.id, email='1231@888555.com', Volunteers_OK__c  = True, GW_Volunteers__Volunteer_Availability__c = 'Friday morning')};
      insert con;
      
     Campaign  CP = new Campaign ( name='TESTCPG' );
     insert CP ;

    GW_Volunteers__Volunteer_Job__c  GMVjob = new GW_Volunteers__Volunteer_job__c ( Name='Testjob', GW_Volunteers__Campaign__c=CP.id );
    insert GMVjob ;

    GW_Volunteers__Volunteer_Shift__c  GMVShift = new GW_Volunteers__Volunteer_Shift__c ( GW_Volunteers__Volunteer_Job__c=GMVjob.id , GW_Volunteers__Start_Date_Time__c =System.now(), GW_Volunteers__Duration__c = 3);
    insert GMVShift ;
      
    PageReference pageRef = Page.Match_Availability;       
    pageRef.getParameters().put('id' , GMVShift.id);
    Test.setCurrentPageReference(pageRef);

       
      wrapperClassController stdCon=new wrapperClassController();
      system.debug('******TEST-stdCon='+ stdCon); 
           
      stdCon.contactList = stdCon.getcontacts();
      for (Integer i = 0; i < 3; i++) {
        system.debug('******TEST-getContacts[' + i + '].con=' + stdCon.ContactList[i].con);
    }
    System.assertEquals(true,stdCon.ContactList[1].con.Volunteers_OK__c);
    
      stdCon.ContactList[1].selected=True;  
           system.debug('******TEST-OPPORTUNITY 1 SELECT stdCon.ContactList[1].selected=' + 
        stdCon.ContactList[1].selected); 
        
         stdCon.processSelected();  
         
    
   }
}

 Thanks!

 

BaguiarBaguiar

Found the issue:

 

all the instances of stdCon.ContactList[1] need to be actually, stdCon.ContactList[0]. Since I've entered only one record on the test methods, it should be [0] right? changed it to [0] and got the 100% coverage.  

 

Thanks for the help as it fixed the portion I couldn;t get fixed!