You need to sign in to do that
Don't have an account?
SFDC9999
Help with Test Class
Hi I need little bit help for Test My TaskListview Controller
public class TaskListViewController { public String objectId {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 TaskListViewController() { } public PageReference refreshPage() { return null; } public List<Task> getTasks() { String ownerId = UserInfo.getUserId(); if (fullTaskList.isEmpty()) { fullTaskList = [SELECT Id, WhatId, WhoId, ActivityDate, subject,Activity_Type__c ,status, priority, Description, ReminderDateTime, IsReminderSet,isClosed FROM Task WHERE isClosed = false AND OwnerId = :ownerId]; numLeft = fullTaskList.size(); total = numLeft; if(numLeft <> 0) this.objectId = ((Task) fullTaskList[0]).id; } displayedTaskList.clear(); if(numLeft <> 0) { 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 void previous() { startNdx -= PAGESIZE; } public void next() { startNdx += PAGESIZE; } public void refreshNumbers() { updateTaskStatus(); nextTask(); this.numLeft = 0; for (Task t : fullTaskList) { if (!t.isClosed) { this.numLeft++; } } } public Boolean getHasNext() { return total > (startNdx + PAGESIZE); } public Boolean getHasPrevious() { return startNdx > 0; } public Integer getNum() { return total; } public PageReference Cancel() { PageReference Activitiespage = Page.Activities; Activitiespage.setRedirect(true); return Activitiespage; return null; } public PageReference save(){ for (Task t : fullTaskList) { update t; PageReference Activitiespage = Page.Activities; Activitiespage.setRedirect(true); return Activitiespage; } return null; } }
@isTest Private Class TestTaskListViewController{ static testMethod void testTaskListViewController() { TaskListViewController t = new TaskListViewController(); t.getTasks(); t.refreshPage(); t.previous(); t.next(); t.refreshNumbers(); t.getHasNext(); t.getHasPrevious(); t.save(); t.getNum(); t.Cancel(); } }
Getting Around 68% Coverage.
I am thinking that if i create a other task and update the status i think my update thing will cover, how would i cover previuos and next page and the page refrence.
Thanks
Use the above code , if this helps please make this as resolved ,and give me kudos.
Thanks,
All Answers
Use the above code , if this helps please make this as resolved ,and give me kudos.
Thanks,
Hi,
Try This
@isTest
Private Class TestTaskListViewController{
static testMethod void testTaskListViewController() {
Account Acc= new Account(Name = 'Test');
insert Acc;
contact con= new contact(Accountid = Acc.id,LastName = 'Test');
insert con;
Task tas = new Task(Subject = 'Test',Whoid = con.id,Whatid = Acc.id,Status = 'Not Started',Priority = 'Normal',Description = 'Test');
insert tas;
TaskListViewController t = new TaskListViewController();
t.getTasks();
t.refreshPage();
t.previous();
t.next();
t.refreshNumbers();
t.getHasNext();
t.getHasPrevious();
t.save();
t.getNum();
t.Cancel();
}
}
Regards,
Rajesh.