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
zeezackisbackzeezackisback 

Apex code - My very first test - what do I do?

I am working on a visual force page, which will display only high priority tasks. Ideally I want this on the home page or as a button. I've got it up and running on sandbox and now discover I need to get it passed 75% test status.

 

How do I do this?

 

I've tried looking around and created a private test class

 

 

please point me in the right direction.

 

Class

 

 

private class SupermanTest { static testMethod void SupermanTest () { DashboardTaskController mySearchCon = new DashboardTaskController(); } } public class DashboardTaskController { //extends DashboardAbstractController public String objectId {get; set;} //public Task currentObject {get; set;} //public String updatedItemStatus{get; set;} public Integer numLeft {get; set;} public Integer total {get; set;} private Integer startNdx = 0; private static Integer PAGESIZE = 8; private List<Task> fullTaskList = new List<Task>(); private List<Task> displayedTaskList = new List<Task>(); public DashboardTaskController() { } public PageReference refreshPage() { return null; } public List<Task> getTasks() { String ownerId = UserInfo.getUserId(); if (fullTaskList.isEmpty()) { fullTaskList = [select id, WhatId, WhoId, ActivityDate, subject, status, priority, Description, ReminderDateTime, IsReminderSet,isClosed from Task where priority = 'High' and isClosed = false and OwnerId = :ownerId]; numLeft = fullTaskList.size(); total = numLeft; this.objectId = ((Task) fullTaskList[0]).id; //this.currentObject = (Task) fullTaskList[0]; } displayedTaskList.clear(); Integer endNdx = startNdx + PAGESIZE; if (endNdx > total) endNdx = total; for (Integer i=startNdx; i<endNdx; i++) displayedTaskList.add(fullTaskList.get(i)); return displayedTaskList; } private void updateTaskStatus() { System.debug('before : ' + fullTaskList); Integer i = 0; for (i=0; i<fullTaskList.size(); i++) { Task t = fullTaskList.get(i); if (this.objectId.equals(t.id)) { System.debug('updating status of ' + t); Task tmp = [select id, WhatId, WhoId, ActivityDate, subject, status, priority, Description, ReminderDateTime, IsReminderSet, isClosed from Task where id = :t.id]; fullTaskList.set(i, tmp); System.debug('updated to ' + tmp); //this.updatedItemStatus = tmp.status; break; } } System.debug('after : ' + fullTaskList); } private void nextTask() { for (Task t : fullTaskList) { if (!t.isClosed) { System.debug('found non-closed object with id ' + t.id); this.objectId = t.id; break; } } } public PageReference showDetail() { // since I can't get the assignTo to work in the VF page/component this.objectId = System.currentPageReference().getParameters().get('objectId'); /* bizarre ... if I call setObjectId, the id returned from the detailPage method is null but if I directly set the id in this method, everything works correctly setObjectId(System.currentPageReference().getParameters().get('objectId')); */ return null; } public void previous() { startNdx -= PAGESIZE; } public void next() { startNdx += PAGESIZE; } public void refreshNumbers() { // the only time this method should be called is when a submit is done // on a record -- we assume the submit closes the record (or at least // puts the record in a state where we are not interested in seeing // it in our todo list anymore updateTaskStatus(); nextTask(); this.numLeft = 0; for (Task t : fullTaskList) { if (!t.isClosed) { this.numLeft++; } } //this.objectId = prefetchedNextObjectId; } public Boolean getHasNext() { return total > (startNdx + PAGESIZE); } public Boolean getHasPrevious() { return startNdx > 0; } public Integer getNum() { return total; } public PageReference save(){ for (Task t : fullTaskList) { update t; } return null; } }

 

 Apex

 

 

<apex:page controller="DashboardTaskController" standardStylesheets="true" showHeader="true"> <!-- close date will be null since we will only display open ones ... but might want to be more flexible here so management view will look different ... see class --> <!-- {!recordType}---{!num} --> <apex:form id="taskList"> <script> function checkIfNeedRefresh() { //alert('checking for full refresh'); var left = document.getElementById('{!$Component.taskList.numberOfItemsLeft}'); //alert('found items left todo : ' + left.innerHTML); var tmp = parseInt(left.innerHTML); if (tmp == NaN || tmp == 0) { parent.refreshPage(); } } </script> <apex:pageBlock id="pageBlock" title="Priority Task List"> <apex:pageBlockTable value="{!tasks}" var="o" id="table"> <apex:column > <apex:commandLink action="{!showDetail}" value="{!o.subject}" reRender="detail"> <apex:param value="{!o.id}" name="objectId" assignTo="{!objectId}"></apex:param> </apex:commandLink> </apex:column> <apex:column id="status" headerValue="Status" value="{!o.status}"/> <apex:column id="priority" headerValue="Priority" value="{!o.priority}"/> <apex:column id="ActivityDate" headerValue="Due Date" value="{!o.ActivityDate}"/> <apex:column id="WhatId" headerValue="Related to" value="{!o.WhatId}"/> <apex:column id="WhoId" headerValue="Contact/Lead" value="{!o.WhoId}"/> <apex:column id="IsReminderSet" headerValue="Reminder" value="{!o.IsReminderSet}"/> <apex:column id="ReminderDateTime" headerValue="Reminder" value="{!o.ReminderDateTime}"/> <apex:column id="Description" headerValue="Comments" value="{!o.Description}"/> </apex:pageBlockTable> </apex:pageBlock> <apex:panelGrid columns="2"> <apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous Page</apex:commandlink> <apex:commandLink action="{!next}" rendered="{!hasNext}">Next Page</apex:commandlink> </apex:panelGrid> <apex:outputText id="numberOfItemsLeft" value="{!numLeft}" style="visibility:hidden"/> <apex:actionFunction action="{!refreshNumbers}" immediate="true" name="refreshNumbers" rerender="numberOfItemsLeft, detail, table" oncomplete="checkIfNeedRefresh();"/> </apex:form> </apex:page>

 

 

 

 

 

soofsoof

The idea is that you need to execute the code in you controller from your Test code, and you need to execute at least 75% of the lines.  Now, you do create an instance of the controller, but what you need to do is call the methods in the controller.  For a start, let's take the getTasks() method.  So after instantiating the controller, you need to do

 

mySearchCon.getTasks();

 

Most of the times, your controller is going to be working with records in the database.  Best practice is to avoid depending on the real data in the database, and create test data in your test code.  You don't need to worry about the data created in your test code, as it is NOT committed to the database.

 

The getTasks() method is querying the Task object and fetching records that the logged in user owns, and whose priority is High.  In your test code, you need to create records that match this criteria, so that when the controller code executes from your test code, it finds the records that it is looking for.

 

After executing the getTasks() method in your test code, call the refreshNumbers() method.  Add calls to all public methods so that you take the code coverage as close to 100% as possible.  To run the tests, right click on your class containing the test code, go to Force.com menu, and click 'Run Tests'.

 

Hope this helps!

 

-soof 

 

zeezackisbackzeezackisback

 

Dear Soof,

I get an error 

 

 Error: Compile Error: Top-level type must have public or global visibility at line 1 column 15

 

 

 

 

private class SupermanTest { static testMethod void SupermanTest () { DashboardTaskController mySearchCon = new DashboardTaskController(); mySearchCon.getTasks(); } }

 

 

 

 

 

wesnoltewesnolte

Hey

 

Move your test code to the end of the class. Or put it into another class with the @isTest annotation, this way the size of the test class will not contribute to your total apex class size limit.

 

Wes 

zeezackisbackzeezackisback

mmm

that didn't work.. placed it at the end it goes

Error: Compile Error: unexpected token: private at line 131 column 0

wesnoltewesnolte

Hey

 

Put it at the very end of your code outside of the class and put the following on the line before the 'private class...'

 

@isTest

 

Wes