You need to sign in to do that
Don't have an account?
jvelazquez
Need Help in apex Test class for Standardset Controller
Hi,
I am writing my first test class for visulaforce page Controller and I am facing problem to cover the test code.
Can any one recommen me how to do. Here is my Apex class and Test class
Apex Class:
public List<Contact> SPFregistered{get; set;} public String filterId{get; set;} private List<SelectOption> listoptions; public Contact con{get; set;} public ApexPages.StandardSetController controller{get; set;} public List<Contact> contact { get { if(controller!= null) return (List<Contact>)controller.getRecords(); else return null ; } set;} public customCtrl(ApexPages.StandardSetController controller) { contact = new List<Contact>() ; con = new Contact() ; } public customCtrl () { }
public PageReference getValues(){ if(filterId == 'All Contacts'){ if(UserInfo.getName() == 'Shane Theriault' || UserInfo.getName() == 'Lavanya Tangati'){ controller = new ApexPages.StandardSetController(Database.getQueryLocator([Select AccountID,FirstName,Heat__c, LastName, Title, Status__c from Contact where Remove__c = false LIMIT 10000])); controller .setPageSize(50); }else if(UserInfo.getName() == 'Michael Lymburner'){
controller = new ApexPages.StandardSetController(Database.getQueryLocator([Select AccountID,FirstName,Heat__c, LastName, Title, Status__c from Contact where Remove__c = false LIMIT 10000])); controller .setPageSize(50);
} }
public Boolean hasNext { get { return controller.getHasNext(); } set; }
public Boolean hasPrevious { get { return controller.getHasPrevious(); } set;
public Integer pageNumber { get { return controller.getPageNumber(); } set; }
}
Test Class:
@isTest private class customCtrlTestClass { static testMethod void validatecustomCtrl1() { List<Contact> lstOfContactsData = new List<Contact>{}; List<User> users = new List<User>(); Account acc1 = new Account(Name='ABC',Sales_Country__c='USA',Site__c='XYZ',Type = 'Prospect',Podster__c = '005e0000000qFNy',SPF_Do_Not_Call__c=false); insert acc1; for(Integer i = 0; i < 10; i++){ Contact c = new Contact(); c.LastName = 'Test Contact'; c.Status__c = 'Active'; c.Site__c = 'Headquarter Chicago'; c.LeadSource=''; c.Lead_Source_Most_Recent_Eloqua__c='Referral'; c.AccountID = acc1.id; c.SPF_Registered__c = false; c.Status__c = 'Active'; c.Remove__c = false; lstOfContactsData.add(c); } insert lstOfContactsData; Id profileid = [Select id from Profile where name = 'System Administrator'].id; User BusinessUser = new User(email='business@test.com', profileid = profileid, UserName='business@test.com', alias='bususer', CommunityNickName='bususer', TimeZoneSidKey='America/New_York', LocaleSidKey='en_US', EmailEncodingKey='ISO-8859-1', LanguageLocaleKey='en_US', FirstName = 'Test2', LastName = 'BusinessUser'); User TechnicalUser = new User(email='technical@test.com', profileid = profileid, UserName='technical@test.com', alias='tecuser', CommunityNickName='tecuser', TimeZoneSidKey='America/New_York', LocaleSidKey='en_US', EmailEncodingKey='ISO-8859-1', LanguageLocaleKey='en_US', FirstName = 'Test5', LastName = 'TechnicalUser'); users.add(BusinessUser ); users.add(TechnicalUser ); //users.add(FinanceUser ); //users.add(TreasuryUser ); insert users; List<Contact> lstOfContacts = [SELECT AccountID,FirstName,Heat__c, LastName, Title, Status__c, Country__c,Email,Phone,Account_Type__c FROM Contact where id In : lstOfContactsData ORDER BY Id Asc]; for(Contact cont : lstOfContacts){ PageReference pageRef = Page.contactsView; pageRef .getParameters().put('id',cont.id); Test.setCurrentPageReference(pageRef); } ApexPages.StandardSetController ctrl = new ApexPages.StandardSetController(lstOfContacts); // List<Contact> contact = (List<Contact>)ctrl.getRecords(); //ctrl.setSelected(contact); customCtrl cc = new customCtrl(ctrl); customCtrl cc1 = new customCtrl(); system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+cc); System.RunAs(BusinessUser){ cc.getValues(); //cc.next(); //cc.previous(); } } }
The code inside the getValues() fucntion is not covering in my test class.
Code doesn look good to me!
You have changed filters based on current user name here, I dont think is a good idea
I am trying to query the records based on filter condtion for different users. Users Shane and Michael see different records in page block table.. which is the reason i have written a If condition..