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
Paul Fitzpatrick 19Paul Fitzpatrick 19 

Apex Unit Tests - Increasing the Code Coverage Percentage

Hi,

I 'inherited' the maintenance of some Apex and Visualforce code that I want to make changes to and "push" using a Change Set from Sandbox to Sandbox and (ultimately) Production. There are Unit Tests associated with the Apex code (which I believe need to be run at a minimum of 75% in order to be able to be deployed to Production) that are currently achieving 74%.

I have not (yet) figured out how to increase the code coverage percentage above 74%.

I have attached the source code for the Apex Class and Apex Unit Test for the class.
 
public with sharing class AttendanceSelectController {

   // ApexPages.StandardSetController must be instantiated
   
   public Date dateToInput{ get; set; }
   public Id selectedProjectCentreId{get;set;}
   
   public List<Attendance__c> attendanceList;
 
   public AttendanceSelectController() {
           dateToInput=Date.Today();
    }
    
    public List<SelectOption> getProjectCentreList()
    {
      List<Project_Centre__c> actList=[select Name,Id from Project_Centre__c];
      List<SelectOption> options = new List<SelectOption>();      
        for (Project_Centre__c pc:actList)
        {
            options.add(new SelectOption(pc.Id,pc.Name));
        }
        return options;
    }
    
    
     public List<SelectOption> getBehaviourItems() {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('1','1'));
            options.add(new SelectOption('2','2'));
            options.add(new SelectOption('3','3'));
            options.add(new SelectOption('4','4'));
            options.add(new SelectOption('5','5'));
            return options;
    }
    
     public Pagereference next()
     {
          return Page.AttendanceInputPage;
     }
    
       public Pagereference Save()
     {
     
         // get rid of the behaviour score if they did not attend
         for (Attendance__c att : attendanceList)
         {
             if( !att.Present__c ){
                 att.Behaviour_Score__c = 0;
             }
         }
       upsert attendanceList;

       PageReference secondPage = Page.DMLThankYou;
       secondPage.setRedirect(true);
      
        return secondPage;
     }
     

     public Pagereference Back()
     {

       PageReference secondPage = Page.AttendanceSelectPage;
       secondPage.setRedirect(true);
      
        return secondPage;
     }
    
     public List<Attendance__c> getAttendanceList() {
   
         attendanceList = new List<Attendance__c>();
         
         
         //get all the attendances for this account and this date and add them to our list
         List<Attendance__c> existingAttendances = [Select Id,Contact__c,Behaviour_Score__c,Date__c,Present__c,Note__c
                                                      from Attendance__c 
                                                      WHERE Date__c = :dateToInput
                                                      And Project_Centre__r.Id = :selectedProjectCentreId
                                                     ];
                                                     
         List<ID> contactIdsDoneAlready = new List<ID>();
         for (Attendance__c existingAtt : existingAttendances)
         {
                attendanceList.add(existingAtt);
                contactIdsDoneAlready.add( existingAtt.Contact__c );
         }
          
          //get all the contacts that dont have an attendance for this date and then give them one
          List<Contact> contactList = [SELECT Name, ID 
                                      FROM Contact 
                                      Where Project_Centre__c = :selectedProjectCentreId 
                                      AND Id not in :contactIdsDoneAlready
                                      and status__c = 'Active'
                                      order by Name];
                                      
                                                          
          for (Contact contact : contactList)
          {
                Attendance__c attendance = new Attendance__c();
                attendance.Contact__c = contact.Id;
                attendance.Behaviour_Score__c = 5;
                attendance.Present__c = true;
                //attendance.Note__c =;
                attendance.Date__c = dateToInput;
                attendance.Project_Centre__c = selectedProjectCentreId;
                attendanceList.add(attendance);
          }

 
        return attendanceList ;
    }
}

And the Apex Test Class
 
@isTest
private class AttendanceControllerTestClass {

    static testMethod void testConstructor() {
        AttendanceSelectController controller = new AttendanceSelectController();
        
        Project_Centre__c testAccount = new Project_Centre__c();
        testAccount.Name = 'test Name';
        insert testAccount;
        
        Contact testContact = new Contact();
        RecordType testRecordType = [Select Id 
            From RecordType 
             where sobjecttype = 'Contact' 
             and name = 'Contact - Service User'];
        testContact.RecordType = testRecordType;
        testContact.Project_Centre__r = testAccount;
        testContact.LastName = 'LastName';
        insert testContact;
        
        controller.selectedProjectCentreId = testAccount.Id;
        
        controller.getProjectCentreList();
        
        controller.next();
        
        controller.back();
        
        controller.getBehaviourItems();
        
        List<Attendance__c> theList = controller.getAttendanceList();
        
        controller.save();
        
        controller.getAttendanceList();
        
        controller.save();
    }

}
Really would appreciate any help on this!

Many Thanks!