+ Start a Discussion
Matt TindallMatt Tindall 

Test Code Help

Iv been trying to write my test code for the following class and i just cant seem to get my head around it.

public with sharing class Total{
	// Staff Variables
		Private integer staff_allowance=252;
    	public integer staff_total_used;
    	public List<Holiday__c> staff_holidays {get;set;}
    // The SOQL without the order and limit
        private String soql {get;set;}
    // The current sort direction. Default to asc
        public String sortDir {
                get { if (sortDir == null) {sortDir = 'asc'; } return sortDir; }
    // Sort the data by the Postcode
		public String sortField {
			get  { if (sortField == null) {sortField = 'Leave_Work__c'; } return sortField;  }
	// format the soql for display on the visualforce page
		public String debugSoql {
			get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }
	// init the controller and display some sample data when the page loads
		public Total() {
			soql = 'SELECT Staff_Memeber__c, Leave_Work__c, Return_to_Work__c, Hours_Used__c, Reason__c, Status__c FROM Holiday__c WHERE Staff_Memeber__c=';
		List<AggregateResult> results=[select sum (Hours_Used__c) FROM Holiday__c WHERE Staff_Memeber__c = 'staff'];
		decimal staff_temp = (decimal)results[0].get('expr0');
		staff_total_used = staff_temp.intvalue();
        // toggles the sorting of query from asc<-->desc
                public void toggleSort() {
                // simply toggle the direction
                        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
                // run the query again
        // runs the actual query
                public void runQuery() {
                	try {
                        staff_holidays = Database.query(soql + '\'staff\'' + ' order by ' + sortField + ' ' + sortDir + ' limit 100');
                	} catch (Exception e) {
                        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
		// The number of hours a staff member is allowed 
    		Public Integer getstaff_allowance() {
        		return staff_allowance;
    	// The total numer or hours already used.
   	 		Public Integer getstaff_total_used() {
        		return staff_total_used;
		// The total number of of hours allowed minus the total number of hours used.
    		Public Integer getstaff_Available() {
        		return staff_allowance-staff_total_used;
		// A working day is based on 9 hours. Divide the available hours by 9 to show the number of whole days available.
    		public integer getstaff_Daysleft() { 
        		return (staff_allowance-staff_total_used)/9;   

 For the purpose of this example im just using one staff member and just refferencing as "staff". Can anyone point me in the right direction with this one. Self taught myself APEX so still learning but getting abit of preasure off the boss now ha.




Hey Matt ,


You can refer this links code, and then try. 





Matt TindallMatt Tindall

What I am trying to get my head around is that am I testing the query that I have ran to add into my LIST. Or is it the integer sums such as:

return staff_allowance-staff_total_used;

That I need to be putting into the test class. If so have you any documentation or links that can help me in the right direction with this?