You need to sign in to do that
Don't have an account?
Hugo Stoupe
Need help writing a test class for an extension
Hi there,
I am really struggling to write 2 test classes for 2 extensions. Is there any good documentation to help write these test classes? The only resource I found was http://www.salesforce.com/docs/developer/pages/Content/pages_controller_error_handling.htm and it isn't helping...
The first extension is called by one VF page, then on success calls a second VF page that has it's own extension. Both extensions follow here.
Extension for 2nd vf page.
I am really struggling to write 2 test classes for 2 extensions. Is there any good documentation to help write these test classes? The only resource I found was http://www.salesforce.com/docs/developer/pages/Content/pages_controller_error_handling.htm and it isn't helping...
The first extension is called by one VF page, then on success calls a second VF page that has it's own extension. Both extensions follow here.
public class openTimeEntry { public dummyTable__c dum{get;set;} public Time_Entry__c emp{get;set;} public ID pID{get;set;} public String eID{get;set;} private Date uDate; public openTimeEntry(ApexPages.StandardController controller) { Date uDate = Date.valueOf(System.Now()); dum=new dummyTable__c(); emp=new Time_Entry__c(); } public PageReference fetchPage(){ String uAlias = UserUtil.CurrentUser.Alias; System.debug('ualias'+uAlias); eID = emp.Employee1__c; System.debug('eID'+eID); if(UserUtil.CurrentUser.Alias=='hsto'||UserUtil.CurrentUser.Alias=='cserv'||UserUtil.CurrentUser.Alias=='pdisc'||UserUtil.CurrentUser.Alias=='data'||UserUtil.CurrentUser.Alias=='edisc'||UserUtil.CurrentUser.Alias=='code'){ if(eid==null){ ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter your name in the Employee field')); return null; } } String testDate = String.valueof(dum.dateOne__c); if(testDate==null) { uDate = Date.today(); }else{ uDate = dum.dateOne__c; } System.debug('openTimeEntryTestDate'+testDate); System.debug('openTimeEntrydumDate'+dum.dateOne__c); System.debug('openTimeEntryuDate'+uDate); System.debug('ProjectID'+pID); //System.debug('useralias:'+uAlias); System.debug('geteID:'+eID); pageReference teGridView = new PageReference('/apex/teGridView?uDate='+uDate+'&pid='+pID+'&eid='+eID); return teGridView; } }
Extension for 2nd vf page.
public class teGridClass { public dummyTable__c dum{get;set;} public Time_Entry__c timecards {get;set;} public Time_Entry__c del; public List<Time_Entry__c> addTEList {get;set;} public List<Time_Entry__c> delTEList {get;set;} public List<Time_Entry__c> TEList {get;set;} public Integer totalCount {get;set;} public Integer rowIndex {get;set;} public String query{get;set;} Public Date tDate{get;set;} Private Date uDate{get;set;} public String pID{get;set;} public String eID{get;set;} public String cID{get;set;} public user currentuser{get;set;} public SFDC_Employee__c currEmployee{get;set;} Private String sDate; // Map of PageParameters private Map<string,string> urlParams=ApexPages.currentPage().getParameters(); // Set the variable in Constructor public List<Time_Entry__c> delTEs {get; set;} public teGridClass(ApexPages.StandardController controller) { pID = apexpages.currentpage().getparameters().get('pID'); eID = apexpages.currentpage().getparameters().get('eID'); cID = apexpages.currentpage().getparameters().get('cID'); if(urlParams.containsKey('uDate')){ sDate = urlParams.get('uDate'); } dum=new dummyTable__c(); System.debug('tDate1'+tDate); tDate = dum.dateOne__c; System.debug('DateOne'+dum.DateOne__c); System.debug('tDate2'+tDate); System.debug('uDate'+uDate); System.debug('sDate'+sDate); timecards = (Time_entry__c)controller.getRecord(); // Temp place Date value if(sDate==null) { tDate = date.ValueOf(System.now()); } else { tDate = date.ValueOf(sDate.substring(0,10)); } // Pull Current User Information currentuser=new User(); currentuser=[Select Id,Name, Alias, Firstname, LastName, Email from User where Id=:userinfo.getuserId()]; // Pull Current Employee Information // IF(currentUser.Alias==' // currEmployee=new SFDC_Employee__c(); // currEmployee=[Select Id,first_name__c from SFDC_Employee__c where User__c=:userinfo.getuserId()]; System.debug('curUserAlias'+currentuser.alias); // System.debug('curempFirstName'+currEmployee.first_name__c); String SobjectApiName = 'Time_Entry__c'; Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe(); Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap(); String commaSepratedFields = ''; for(String fieldName : fieldMap.keyset()){ if(commaSepratedFields == null || commaSepratedFields == ''){ commaSepratedFields = fieldName; }else{ commaSepratedFields = commaSepratedFields + ', ' + fieldName; } } query = 'select ' + commaSepratedFields + ' from ' + SobjectApiName + ' Where Date__c=:tDate'; if(eID.length()>6) //Do something with eID { query = query+' AND Employee1__c=:eID'; currEmployee=new SFDC_Employee__c(); currEmployee=[Select Id,first_name__c from SFDC_Employee__c where first_name__c=:userinfo.getfirstname() AND last_name__c=:userinfo.getlastname()]; }else{ // Look up employeeID based on user ID try{ List<SFDC_Employee__c> E=[Select Id,first_name__c from SFDC_Employee__c where User__c=:userinfo.getuserId()]; if (!E.isEmpty()) { }Else{ currEmployee=new SFDC_Employee__c(); currEmployee=[Select Id,first_name__c from SFDC_Employee__c where User__c=:userinfo.getuserId()]; } }catch(Exception ex){ ApexPages.addMessages(ex); } query = query+''; } if(pID.length()>6) //Do something with pID { query = query+' AND Project__c=:pID'; }else{ query = query+''; } query = query+' LIMIT 10'; System.debug('query:'+query); System.debug('uDate:'+uDate); System.debug('projectID:'+pID); System.debug('EmpID:'+eID); System.debug('Esize: ' + eID.length() ); System.debug('psize: ' + pID.length() ); System.debug('DateOne'+dum.DateOne__c); System.debug('tDate2'+tDate); System.debug('uDate'+uDate); System.debug('sDate'+sDate); TEList = Database.query(query); totalCount = TEList.size(); System.debug('totalcount'+totalCount); IF(totalCount==0){ addRow(); } delTEList = new List<Time_Entry__c>(); delTEs = new List<Time_Entry__c>(); } public void addRow(){ currEmployee=new SFDC_Employee__c(); currEmployee=[Select Id,first_name__c from SFDC_Employee__c where User__c=:userinfo.getuserId()]; eID=currEmployee.ID; System.debug('query:'+query); System.debug('uDate:'+uDate); System.debug('projectID:'+pID); System.debug('EmpID:'+eID); System.debug('Esize: ' + eID.length() ); System.debug('psize: ' + pID.length() ); System.debug('DateOne'+dum.DateOne__c); System.debug('tDate2'+tDate); System.debug('uDate'+uDate); System.debug('sDate'+sDate); addTEList = new List<Time_Entry__c>(); TEList.add(new Time_Entry__c(Date__c = tDate, project__c = pid, contract__c = cid, employee1__c=eid)); } public PageReference ave(){ try{ upsert TEList; delete delTEList; return (new ApexPages.StandardController(timecards)).view(); }catch(Exception ex){ ApexPages.addMessages(ex); } return null; } public void deleteRow(){ rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex')); System.debug('rowbe deleted ' + rowIndex ); System.debug('rowm to be deleted '+TEList[rowIndex]); del = TEList.remove(rowIndex); delTEList.add(del); } // } }
for your first class, you can use the follwoing as reference. I assume the standardard controller is Account. If not, replace with the one you use.
replace bold char with your value.
test for the second class would be similar.
@isTest
public class openTimeEntry_Test
{
public testmethod static void fetchPage_redirect()
{
user usr1 = [select id from user where alias = 'abc'];
Account acc = new Account(name='test');
insert acc;
ApexPages.StandardController sc = new ApexPages.StandardController(acc);
system.runas(usr);
openTimeEntry oTE = new openTimeEntry(sc);
system.assertequals(expectedata,oTE.geturl());
}
public testmethod static void fetchPage_noredirect()
{
user usr1 = [select id from user where alias = 'hsto']; // this will cause return null, or the current page.
Account acc = new Account(name='test');
insert acc;
ApexPages.StandardController sc = new ApexPages.StandardController(acc);
system.runas(usr);
openTimeEntry oTE = new openTimeEntry(sc);
system.assertequals(currentpageurl,oTE.geturl());
}
}
I tried this based on your suggestion, but I got 2 failed tests: Both are the same errors:
Error Message System.NullPointerException: Attempt to de-reference a null object
Stack Trace Class.openTimeEntry_Test2.fetchPage_noredirect: line 25, column 1
(Line 25 is "String nextPage=ote.fetchPage().getUrl();")
Any thoughts???
Line 6 indicates you use user with alias 'hsto', from the code, it returns null. (just like Bogdan mentioned). you need to use another user.
You may change line 26 to
system.assertequals(null,ote.fetchPage());
and remove line 25.
David