You need to sign in to do that
Don't have an account?
Test class for Custom list controller
Hi ,
I have written this class for custom list button :
public with sharing class ContactPortalUserController
{
List<User>luser = new List<User>();
List<contact>selectedCon = new List<contact>();
ID profileID = [select id from profile where name = 'High Volume Customer Portal'].id;
private ApexPages.StandardSetController standardController;
// ApexPages.Message myMsg = new ApexPages.Message('info', summary);
public ContactPortalUserController(ApexPages.StandardSetController standardController)
{
this.standardController = standardController;
}
public PageReference selectedContact(){
List<Contact>selectedContact = (List<Contact>) standardController.getSelected();
selectedCon = [select id , name , firstname, email ,EmailEncoding__c,LanguageLocaleKey__c,TimeZoneSidKey__c,LocaleSidKey__c, lastname from contact where id in:selectedContact];
for(contact con:selectedCon)
{
User us = new User();
us.alias = con.Firstname.substring(0,3) +'cus';
us.email = con.email;
us.emailencodingkey= con.EmailEncoding__c;
us.lastname = con.lastname;
us.languagelocalekey = con.LanguageLocaleKey__c;
us.localesidkey =con.LocaleSidKey__c;
us.profileid = profileID;
us.contactId = con.id;
us.timezonesidkey = con.TimeZoneSidKey__c;
us.username = con.email +'cus';
luser.add(us);
}
try{
insert luser;
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Info, 'Users have been inserted successfully.');
ApexPages.addmessage(myMsg);
// myMsg = ;
//ApexPages.addMessage(myMsg);
}
catch(exception e)
{
e.getmessage();
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error Occured while inserting users,' + e.getmessage());
ApexPages.addMessage(myMsg);
}
return null;
}
}
I am writing test class for above controller:
public class ContactPortalUserControllerTest{
public static testMethod void testContactPortalUserController() {
List<contact> lcontact = new List<contact>();
Contact con = new contact();
con.lastname = 'test';
con.email = 'Test@accenture.com';
insert con;
lcontact.add(con);
PageReference pageRef = Page.ContactPortalUser;
Test.setCurrentPage(pageRef);
// ContactPortalUserController controller = new ContactPortalUserController (null);
// controller.selectedContact = lcontact;
// controller.selectedContact();
ApexPages.StandardController sc = new ApexPages.StandardController(con);
ContactPortalUserController controller = new ContactPortalUserController (null);
controller.selectedContact();
}
}
I am not sure how to set values for selected contact, can anybody help me with this.
Thanks
Ritika
Hi,
In the test method itself you need to create a contact record and assign that record to selected contact, I think this resolve your problem.
Regards,
Sridhar Bonagiri
Thanks for your reply , but i am not able to set value of selected contact.
I have created new contact and added it to list lcontact , but i am not able pass this list to controller it gives me error.
Can you help me , how to pass this value.
Hi,
Please let me know the error that you are getting.
Regards,
Sridhar Bonagiri
Hi,
When i am passing the list of selected contact, it is giving me error
Code:
ContactPortalUserController controller = new ContactPortalUserController (null);
controller.setSelected(lcontact);
controller.selectedContact();
Error:Error: Compile Error: Method does not exist or incorrect signature: [ContactPortalUserController].setSelected(LIST<Contact>) at line 22 column 2
- I study your code and get to know that you are getting the error.
- Because you are using StandardController in your Test class.
- You need to use the StandardSet controller then it definately works.
Please, let me know if still getting errors.Thanks
Cheers