You need to sign in to do that
Don't have an account?
Oldwen Adriano
Need help writing a Test class for this class
Could someone please give me a hand writing a test class for this class below?
-----------------------------------------------------------------
global class ControllerGetNext {
List<Task> tList = new List<Task>();
List<wrapper> lstw = new List<wrapper>();
List<wrapper> lstwPage = new List<wrapper>();
//----------------------------------------------------------
//Campaign
List<Campaign__c> cList = new List<Campaign__c>();
public string userselected{get;set;} //I have it declared here as a public variable in the controller
public List<selectOption> useroptions{get;set;}
//----------------------------------------------------------
public ControllerGetNext()
{
// integer nextcount=0;
nextvisible = false;
previsible = true;
page=0;
system.debug('nextvisible=='+nextvisible);
//----------------------------------------------------------
//Campaign
//When the controller instatiates this is called as expected.
useroptions = new list<selectoption>();
cList = [Select ID, Campaign_Name__c from Campaign__c];
for(integer i=0; i < cList.size(); i++)
{
useroptions.add(new SelectOption(cList[i].ID, cList[i].Campaign_Name__c));
}
//----------------------------------------------------------
}
Contact cContact;
Lead lLead;
public List<wrapper> lstWrapperString{
get{return getTasks();}
}
//lstWrapperString
//getTasks
Public List<wrapper> getTasks()
{
lstw.clear();
tList = [Select ID, Subject, ActivityDate, Due_Time__c, Description, WhoID from Task where OwnerId =: UserInfo.getUserId() And Status != 'Completed']; //All of the users tasks
if(!tList.isEmpty())
{
for(integer i=0; i < tList.size(); i++) //Loop thru the tasks and add to list
{
string strPhone = '';
string LeadName = '';
decimal iAttepmts = 0;
try
{
cContact = [SELECT Phone FROM Contact WHERE Id = : tList[i].WhoID]; //The phone is in the contact object which is related to the Lead
strPhone = cContact.Phone;
}
catch(exception e)
{
strPhone = 'Not Provided';
}
try
{
lLead = [SELECT Name, Attempts__c FROM Lead WHERE Id = : tList[i].WhoID];
LeadName = lLead.Name;
iAttepmts = lLead.Attempts__c;
}
catch(exception e)
{
LeadName = 'Not Provided';
iAttepmts = 0;
}
if(iAttepmts < 20) //Business rule for less than 20 attemps.
{
lstw.add(new wrapper(tList[i].ID, LeadName, tList[i].ActivityDate, tList[i].Due_Time__c, tList[i].Description, strPhone));
}
}
}
return lstw;
}
//*******************************************************************************************************
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//Create my wrapper class... like an ENUM
Public class wrapper
{
public string tID{get;set;}
public string tSubject{get;set;}
public date tActivityDate{get;set;}
public string tDueTime{get;set;}
public string tDescription{get;set;}
public string tPhone{get;set;}
//Constructor
public wrapper(string tID, string tSubject, date tActivityDate, string tDueTime, string tDescription, string tPhone)
{
this.tID = tID;
this.tSubject = tSubject;
this.tActivityDate = tActivityDate;
this.tDueTime = tDueTime;
this.tDescription = tDescription;
this.tPhone = tPhone;
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//***********************************************************
//Page navigation actions
//***********************************************************
public integer page=0;
public boolean exfor = true;
public integer nextcount=0;
public List<integer> previouscount = new Integer[100];
public boolean previsible{get;set;}
public boolean nextvisible{get;set;}
public pagereference NextPage()
{
page ++;
exfor = true;
lstwPage = getTasks();
return null;
}
public pagereference previousPage()
{
page--;
nextcount = previouscount.get(page);
system.debug('nextcount'+nextcount);
exfor = true;
lstwPage = getTasks();
return null;
}
//**********************************************************
webService static String GetNextProcess(string ddlCampaignParam) {
//Lead variable used to assign it
Lead assignLead = null;
boolean foundLead = false;
Area_Code__c myTimeZone;
User myUser;
string strLead;
string workPhone;
string myArea;
string strTimeZone;
//Twice in one day variables
Date callDate1;
Date callDate2;
boolean setDate1 = false;
boolean setDate2 = false;
Campaign__c myCamp;
try
{
//Lets get the ID for the dserv user to be used in our query
myUser = [Select Id from User Where Alias = 'dserv'];
//Lets get the name of the Campaign
myCamp = [Select Campaign_Name__c from Campaign__c Where ID =: ddlCampaignParam];
//return 'Error: User name is: ' + UserInfo.getUserId();
//****************************************************************************
//Rule #1 : Pull all the leads that have not been called 4 times or more yet and order then from least to greatest.
//****************************************************************************
list<Lead> lstLeadToAssign = [Select Id, Phone, Lead_Start_Time__c, Lead_End_Time__c from Lead Where (Attempts__c < 5 Or Attempts__c = null) And Final_Flag__c != true And PCB_Flag__c != true And OwnerId =: myUser.Id And Campaign__c =: myCamp.Campaign_Name__c Order By Attempts__c ASC Limit 50];
//return 'Error: Lead size is : ' + lstLeadToAssign.size();
if(lstLeadToAssign.IsEmpty() == false)
{
for(integer i=0; i < lstLeadToAssign.size(); i++)
{
try
{
//----------------------------------------------------------------
//Get the phone num so we can check timezone for call availability against area code table.
//-----------------------------------------------------------------
try
{
workPhone = lstLeadToAssign[i].Phone;
//return 'Error: I got here after phone' + workPhone;
myArea = workPhone.substring(1, 4); //assumes the format is (210)
//return 'Error: I got here ';
}
catch(exception e)
{
continue;
}
try
{
myTimeZone = [Select TimeZone__c from Area_Code__c Where Name =: myArea];
strTimeZone = myTimeZone.TimeZone__c;
}
catch(exception e)
{
return 'Error: No Time Zone info in Area code table -- ' + e;
}
//****************************************************************************
//Rule #2 : Check to make sure that lead is in a callable time zone.
//****************************************************************************
if(AllowedZone(strTimeZone) == true)
{
//return 'Error: I got here';
foundLead = true;
strLead = lstLeadToAssign[i].Id;
//***************************************************************************************
//Rule- only twice in one day : Check to make sure.
//***************************************************************************************
if(lstLeadToAssign[i].Lead_Start_Time__c != null)
{
callDate1 = lstLeadToAssign[i].Lead_Start_Time__c;
if(callDate1 == date.today())
{
//This means, they have been called once today already. So we will check to see if they've been called twice.
if(lstLeadToAssign[i].Lead_End_Time__c != null)
{
callDate2 = lstLeadToAssign[i].Lead_End_Time__c;
if(callDate2 == date.today())
{
//They have already been called twice today so we move on to the next lead.
foundLead = false;
}
else
{
setDate2 = true;
}
}
else
{
setDate2 = true;
setDate1 = false;
}
}
else
{
setDate1 = true;
}
}
else
{
//If Date from Start time was null, then we will set this as our first date.
setDate1 = true;
}
//***************************************************************************************
}
//If lead was found lets go ahead and assign it to the user and and return it and end the process.
if(foundLead == true)
{
assignLead = [Select Id, Lead_Start_Time__c, Attempts__c, Lead_End_Time__c from Lead Where Id =: strLead for update];
try
{
assignLead.OwnerId = UserInfo.getUserId();
//return 'Error: Lead.OwnerId - ' + assignLead.OwnerId;
}
catch(exception e)
{
return 'Error: Could not find your user information!';
}
//***************************************************************************
//Assign our 24 hour period variables
//***************************************************************************
try
{
if(setDate1 == true)
{
assignLead.Lead_Start_Time__c = date.today();
}
if(setDate2 == true)
{
assignLead.Lead_End_Time__c = date.today();
}
}
catch(exception e)
{
return 'Error: On 24 hour date reference! - ' + e;
}
//***************************************************************************
if(assignLead.Attempts__c == null)
{
assignLead.Attempts__c = 1;
}
else
{
assignLead.Attempts__c = assignLead.Attempts__c + 1;
}
update assignLead;
return strLead;
}
}
catch(exception e)
{
return 'Error: inside the last lead found!' + e;
}
}
}
}
catch(exception e)
{
return 'Error: Currently, there is no Lead to assign!';
}
return 'Error: Currently, there is no Lead to assign!';
}
//Function to determine which timezones are currently allowed for calling
public static boolean AllowedZone(string myZone)
{
//return true;
DateTime myDate = DateTime.Now(); //Since the server lives in the EST timezone, you will get an EST time
Integer myStart = 9; //Calls start at 9am
Integer myEnd = 16; //Cannot call later than 5pm
Integer myHour = myDate.Hour(); //Get the current hour.
//Adjust the hour based on the time zone that was passed. EST is the base time zone.
if(myZone == 'CST')
{
myHour = myHour - 1;
}
if(myZone == 'MST')
{
myHour = myHour - 2;
}
if(myZone == 'PST')
{
myHour = myHour - 3;
}
if(myHour < myStart || myHour > myEnd)
{
return false;
}
else
{
return true;
}
}
}
--------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------
global class ControllerGetNext {
List<Task> tList = new List<Task>();
List<wrapper> lstw = new List<wrapper>();
List<wrapper> lstwPage = new List<wrapper>();
//----------------------------------------------------------
//Campaign
List<Campaign__c> cList = new List<Campaign__c>();
public string userselected{get;set;} //I have it declared here as a public variable in the controller
public List<selectOption> useroptions{get;set;}
//----------------------------------------------------------
public ControllerGetNext()
{
// integer nextcount=0;
nextvisible = false;
previsible = true;
page=0;
system.debug('nextvisible=='+nextvisible);
//----------------------------------------------------------
//Campaign
//When the controller instatiates this is called as expected.
useroptions = new list<selectoption>();
cList = [Select ID, Campaign_Name__c from Campaign__c];
for(integer i=0; i < cList.size(); i++)
{
useroptions.add(new SelectOption(cList[i].ID, cList[i].Campaign_Name__c));
}
//----------------------------------------------------------
}
Contact cContact;
Lead lLead;
public List<wrapper> lstWrapperString{
get{return getTasks();}
}
//lstWrapperString
//getTasks
Public List<wrapper> getTasks()
{
lstw.clear();
tList = [Select ID, Subject, ActivityDate, Due_Time__c, Description, WhoID from Task where OwnerId =: UserInfo.getUserId() And Status != 'Completed']; //All of the users tasks
if(!tList.isEmpty())
{
for(integer i=0; i < tList.size(); i++) //Loop thru the tasks and add to list
{
string strPhone = '';
string LeadName = '';
decimal iAttepmts = 0;
try
{
cContact = [SELECT Phone FROM Contact WHERE Id = : tList[i].WhoID]; //The phone is in the contact object which is related to the Lead
strPhone = cContact.Phone;
}
catch(exception e)
{
strPhone = 'Not Provided';
}
try
{
lLead = [SELECT Name, Attempts__c FROM Lead WHERE Id = : tList[i].WhoID];
LeadName = lLead.Name;
iAttepmts = lLead.Attempts__c;
}
catch(exception e)
{
LeadName = 'Not Provided';
iAttepmts = 0;
}
if(iAttepmts < 20) //Business rule for less than 20 attemps.
{
lstw.add(new wrapper(tList[i].ID, LeadName, tList[i].ActivityDate, tList[i].Due_Time__c, tList[i].Description, strPhone));
}
}
}
return lstw;
}
//*******************************************************************************************************
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//Create my wrapper class... like an ENUM
Public class wrapper
{
public string tID{get;set;}
public string tSubject{get;set;}
public date tActivityDate{get;set;}
public string tDueTime{get;set;}
public string tDescription{get;set;}
public string tPhone{get;set;}
//Constructor
public wrapper(string tID, string tSubject, date tActivityDate, string tDueTime, string tDescription, string tPhone)
{
this.tID = tID;
this.tSubject = tSubject;
this.tActivityDate = tActivityDate;
this.tDueTime = tDueTime;
this.tDescription = tDescription;
this.tPhone = tPhone;
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//***********************************************************
//Page navigation actions
//***********************************************************
public integer page=0;
public boolean exfor = true;
public integer nextcount=0;
public List<integer> previouscount = new Integer[100];
public boolean previsible{get;set;}
public boolean nextvisible{get;set;}
public pagereference NextPage()
{
page ++;
exfor = true;
lstwPage = getTasks();
return null;
}
public pagereference previousPage()
{
page--;
nextcount = previouscount.get(page);
system.debug('nextcount'+nextcount);
exfor = true;
lstwPage = getTasks();
return null;
}
//**********************************************************
webService static String GetNextProcess(string ddlCampaignParam) {
//Lead variable used to assign it
Lead assignLead = null;
boolean foundLead = false;
Area_Code__c myTimeZone;
User myUser;
string strLead;
string workPhone;
string myArea;
string strTimeZone;
//Twice in one day variables
Date callDate1;
Date callDate2;
boolean setDate1 = false;
boolean setDate2 = false;
Campaign__c myCamp;
try
{
//Lets get the ID for the dserv user to be used in our query
myUser = [Select Id from User Where Alias = 'dserv'];
//Lets get the name of the Campaign
myCamp = [Select Campaign_Name__c from Campaign__c Where ID =: ddlCampaignParam];
//return 'Error: User name is: ' + UserInfo.getUserId();
//****************************************************************************
//Rule #1 : Pull all the leads that have not been called 4 times or more yet and order then from least to greatest.
//****************************************************************************
list<Lead> lstLeadToAssign = [Select Id, Phone, Lead_Start_Time__c, Lead_End_Time__c from Lead Where (Attempts__c < 5 Or Attempts__c = null) And Final_Flag__c != true And PCB_Flag__c != true And OwnerId =: myUser.Id And Campaign__c =: myCamp.Campaign_Name__c Order By Attempts__c ASC Limit 50];
//return 'Error: Lead size is : ' + lstLeadToAssign.size();
if(lstLeadToAssign.IsEmpty() == false)
{
for(integer i=0; i < lstLeadToAssign.size(); i++)
{
try
{
//----------------------------------------------------------------
//Get the phone num so we can check timezone for call availability against area code table.
//-----------------------------------------------------------------
try
{
workPhone = lstLeadToAssign[i].Phone;
//return 'Error: I got here after phone' + workPhone;
myArea = workPhone.substring(1, 4); //assumes the format is (210)
//return 'Error: I got here ';
}
catch(exception e)
{
continue;
}
try
{
myTimeZone = [Select TimeZone__c from Area_Code__c Where Name =: myArea];
strTimeZone = myTimeZone.TimeZone__c;
}
catch(exception e)
{
return 'Error: No Time Zone info in Area code table -- ' + e;
}
//****************************************************************************
//Rule #2 : Check to make sure that lead is in a callable time zone.
//****************************************************************************
if(AllowedZone(strTimeZone) == true)
{
//return 'Error: I got here';
foundLead = true;
strLead = lstLeadToAssign[i].Id;
//***************************************************************************************
//Rule- only twice in one day : Check to make sure.
//***************************************************************************************
if(lstLeadToAssign[i].Lead_Start_Time__c != null)
{
callDate1 = lstLeadToAssign[i].Lead_Start_Time__c;
if(callDate1 == date.today())
{
//This means, they have been called once today already. So we will check to see if they've been called twice.
if(lstLeadToAssign[i].Lead_End_Time__c != null)
{
callDate2 = lstLeadToAssign[i].Lead_End_Time__c;
if(callDate2 == date.today())
{
//They have already been called twice today so we move on to the next lead.
foundLead = false;
}
else
{
setDate2 = true;
}
}
else
{
setDate2 = true;
setDate1 = false;
}
}
else
{
setDate1 = true;
}
}
else
{
//If Date from Start time was null, then we will set this as our first date.
setDate1 = true;
}
//***************************************************************************************
}
//If lead was found lets go ahead and assign it to the user and and return it and end the process.
if(foundLead == true)
{
assignLead = [Select Id, Lead_Start_Time__c, Attempts__c, Lead_End_Time__c from Lead Where Id =: strLead for update];
try
{
assignLead.OwnerId = UserInfo.getUserId();
//return 'Error: Lead.OwnerId - ' + assignLead.OwnerId;
}
catch(exception e)
{
return 'Error: Could not find your user information!';
}
//***************************************************************************
//Assign our 24 hour period variables
//***************************************************************************
try
{
if(setDate1 == true)
{
assignLead.Lead_Start_Time__c = date.today();
}
if(setDate2 == true)
{
assignLead.Lead_End_Time__c = date.today();
}
}
catch(exception e)
{
return 'Error: On 24 hour date reference! - ' + e;
}
//***************************************************************************
if(assignLead.Attempts__c == null)
{
assignLead.Attempts__c = 1;
}
else
{
assignLead.Attempts__c = assignLead.Attempts__c + 1;
}
update assignLead;
return strLead;
}
}
catch(exception e)
{
return 'Error: inside the last lead found!' + e;
}
}
}
}
catch(exception e)
{
return 'Error: Currently, there is no Lead to assign!';
}
return 'Error: Currently, there is no Lead to assign!';
}
//Function to determine which timezones are currently allowed for calling
public static boolean AllowedZone(string myZone)
{
//return true;
DateTime myDate = DateTime.Now(); //Since the server lives in the EST timezone, you will get an EST time
Integer myStart = 9; //Calls start at 9am
Integer myEnd = 16; //Cannot call later than 5pm
Integer myHour = myDate.Hour(); //Get the current hour.
//Adjust the hour based on the time zone that was passed. EST is the base time zone.
if(myZone == 'CST')
{
myHour = myHour - 1;
}
if(myZone == 'MST')
{
myHour = myHour - 2;
}
if(myZone == 'PST')
{
myHour = myHour - 3;
}
if(myHour < myStart || myHour > myEnd)
{
return false;
}
else
{
return true;
}
}
}
--------------------------------------------------------------------------------------------------------------------------
Each test should follow the following structure:
- Setup of test data. This includes creation of any data needed by your class. Account, Contacts etc
- Starting the test. This is calling Test.startTest() to reset the governor limits
- Calling your class / method
- Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
- Asserting that your changes have worked
- If you have inserted/updated/deleted data, you need to query for the updates
- Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.Also, in the future, please use the "Add a code sample" button (icon <>) to increase code readability. Also, try to ask specific questions about issues you have when writing tests, not just "write my tests for me"
[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[4] http://blog.deadlypenguin.com/blog/testing/strategies/
Also, in the future, please note that any posts from me asking for help is just that. I'm asking for help, not for anyone to do the work for me. I am looking for guidance, collaboration, ideas, and suggestions from experienced SFers.
If you have any guidance for me for this particular class, I am a willing sponge.
Thank you
With that out of the way, the fourth link I included above talks about strategies for testing complex code and would be easily applied to your code you included. You would create individual test methods for the following methods
- Constructor
- 1stWrapperString
- Wrapper Constructor
- nextPage
- previousPage
- getNextProcess
- allowedZone
Then you'll create multiple version of each of those tests to cover each of your facets. For example a CST, PST and MST test for allowedZone. Of these you'll have the most for getNextProcess.You do not (and should not) have one giant test for all of these methods.
On another note, I have a question you may very well know. I am using the Eclipse IDE and when I run my tests, I do not see my class in the list. It appears to only be capturing my triggers. Do you know how I might resolve this?
Thank you.
As for the Eclipse question, I do not know. I do not use Eclipse and run all of my tests via either the developer console or via setup -> develop -> apex test execution.
[1] https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_testvisible.htm
Thanks.....
Save error: Static methods cannot be invoked through an object instance
The error occurs on the objGetNext.AllowedZone();
Thank you!
To test the wrapper class, you would do just like you did for your normal constructor except you would call it one level deeper
Thanks,
Oldwen
Then to make sure it works for multiple pages
Then do something similar for the previousPage method.
I would recommend adding a test for previousPage when your on page 0 and a nextPage test when you're on the last page. Just to make sure they do the right things.
I'm happy to report that I have 93% code coverage accross all my classes and you were a wonderful help to allow me to accomplish that. I would like to call upon your expertise again. Could you please take a look at a new questions I posted and see if you can help?
Thank you very much....
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BSSiIAO