function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Susheel Reddy BSusheel Reddy B 

Need help with Test class

Hi,

I have issues in writing test class for below copntroller.
 
public class EditContacts {

    ApexPages.StandardSetController setCon;
    List<string> strIds = new List<string>();
    List<Contact> lstContact = new List<Contact>();
    public integer size {get;set;}
    public final String URL {get;set;}
    public Contact c;
    
    public Contact getNewContact()
    {
      if(c==null)c= new Contact();
      return c;
    }
    public MassEditContacts (Apexpages.Standardsetcontroller cont)
    {
        setCon = cont;
        strIds = ApexPages.currentPage().getParameters().get('recs').split(',',-2);
        size = strIds.size();
        URL = ApexPages.currentPage().getURL();
    
    }
    public List<Contact> getContacts()
    {
        lstContact = [select Name, Title, Phone, MobilePhone, Email, AccountId, ownerid from Contact where id IN: strIds ];
        return lstContact;
    }
     
    public Pagereference saveRecords()
    {
        List<Contact> updateContact = new List<Contact>();
    for(Contact cse : lstContact)
    {
      updateContact.add(cse);
    }
        update updateContact;
        return new Pagereference('/'+Contact.getSObjectType().getDescribe().getKeyPrefix()+'/o');
    }
    
    public Pagereference ccancel()
    {
        system.debug('***ccancel**');
        return new Pagereference('/'+Contact.getSObjectType().getDescribe().getKeyPrefix()+'/o');
    }
}

 
James LoghryJames Loghry
If you could post the issues that you're having along with either a sample or the entire test class as well, perhaps we could help.
Salesforce WizardSalesforce Wizard
Generally speaking, your test method would look something like this:

//Create all the test Data
//Call Controller
//Set Visualforce Page
//Call Controller Methods
//Assert Methods perform expected output

You can see a generic example of testing a visualforce controller here: http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm

The one it doesn't do is create any test data. But that's works just like creating a new record outside of a test. Just make sure you create all the data you need since your test won't see any actual live data (and you don't want it to)
Susheel Reddy BSusheel Reddy B
Below is my test Class
Error: Compile Error: Constructor not defined: [MassEditContacts].<Constructor>(ApexPages.StandardController) at line 21 column 24
@isTest
public class TaskControllerExtensionTest
{
Static TestMethod void testSave()
{
Test.startTest();
Account acc = new Account();
acc.Name = 'test account';
insert acc;
Contact cont = new Contact();
cont.firstname = 'test';
cont.lastname = 'contact';
cont.email = 'test@contact.com';
insert cont;

PageReference ref = Page.MassEditContact;
Test.setCurrentPage(ref);
ref.getParameters().put('Ids',cont.id);
Contact con = new Contact();
ApexPages.StandardController ctrl = new ApexPages.StandardController(con);
MassEditContacts ext = new MassEditContacts(ctrl);
List<Contact> tList = ext.getIds();
System.assertEquals(tList.size(),1);
PageReference pg = ext.Save();
ext.getContacts();
ext.ccancel();
if(pg != null) System.assertNotEquals(pg, null);
Test.stopTest();
}
}
James LoghryJames Loghry
Your MassEditContacts class takes in a StandardSetController has a param, not a StandardController.

So you'll need to instantiate a StandardSetController first (which takes a list of sobjects and not a single sobject).

For instance, the following should solve the error above:
 
ApexPages.StandardSetController ctrl = new ApexPages.StandardSetController(new List<Contact>{con});
MassEditContacts ext = new MassEditContacts(ctrl);