+ Start a Discussion
BaguiarBaguiar 

Wrapperclass test issues

Hi there,

 

still strugling with the test method for this wrapperclass as I can't pass 30% coverage. HAd some good help but still fighting it! :)

 

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 Method:

 

@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();  
         
    
   }
}

 HELP! :)

bob_buzzardbob_buzzard

Can you mark the lines that are showing as not covered?

BaguiarBaguiar

SUre.. Actually I have 56% Coverage. whats RED is NOT covered.

 

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; } } }

 Thanks!

bob_buzzardbob_buzzard

That's most odd - you are invoking this method so I'd expect at least the signature and the initial iteration to be covered.

 

Does the unit test throw any errors?

BaguiarBaguiar

This is the message from Test Failure:

 

TestwrapperClassController.testgetContacts736.0System.ListException: List index out of bounds: 1Class.TestwrapperClassController.testgetContacts: line 34, column 84 External entry point



I thought I was invoking it through:    stdCon.processSelected()

The very last line..

BaguiarBaguiar

Found the Error!

 

The ContactList error that was giving me was out of bounds [1]. I beleive since I enetered only one test record, it should be [0] correct?

 

Well, I've changed it to [0] in all the instancesof stdCon.ContatcList and have 100% covergae now.

 

Thanks Bob for the help!

bob_buzzardbob_buzzard

I'd expect the issue to be with these lines:

 

 for (Integer i = 0; i < 3; i++) {
        system.debug('******TEST-getContacts[' + i + '].con=' + stdCon.ContactList[i].con);
    }

 as you are assuming there will be three values returned, but the error indicates there is only one.

 

This would do better to iterate the actual list size:

 

 for (Integer i = 0; i < stdCon.ContactList.size(); i++) {
        system.debug('******TEST-getContacts[' + i + '].con=' + stdCon.ContactList[i].con);
    }

 

also, later on you are accessing the element at index 1 of the contactlist - I'd expect this to fail in the same way as the list will has only one element, at index 0.

bob_buzzardbob_buzzard

Hah.  Cross post!

 

Glad to see you got there.

BaguiarBaguiar

Got it. Thought that   i<3  would work since that was only one test record entered for the contacts. I left the line that way as I've copied from another test class I had and it was working fine. but... I see what you are saying. Thanks as usual Bob!

BaguiarBaguiar

Bob, this is crazy..

NOthing was modified on my code, got the class with test methos passed to production with 100% coverage. NOw, 01 month later, I was trying to enter a new class into production and the system is telling me now that the coverage on this test method is now 30% !! NO changes made at all.  Went to my sand box on friday, and the coverage was 100 %. SAME EXACT CODE. Here comes the fun part. Came this morning to start working on it and, by miracle, the test method on the sandbox went to 30% !! How can this happen without me changing anything on the code ?

bob_buzzardbob_buzzard

The only way that I've seen that sort of thing happen before is when a validation rule or required field has been introduced that breaks existing tests.

BaguiarBaguiar

Bob... found it and it is one of those things that are allways suggested when hardcoding dates. I've set one of the values to "Wednesday morning" on the test contact to be added. On another part of the test I've used "System.now()". Since today is Monday, it will fail for sure.

 

Should I hardcode a date taht will allways be "wednesday morning" like  08/17/2011, and if so, should the test data entering look like this:

 

GW_Volunteers__Start_Date_Time__c = '2011-08-17 9:00:21'

 

Thanks as usual!

B

bob_buzzardbob_buzzard

Sounds like a reasonable approach to me.