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
rivereridanusrivereridanus 

Need help with custom controller test case

Hello!

I have a custom controller that I got online and customized for my own environment and use. I have tested it personally and I think it's ready for prod. But I don't know how to write a test case for this type of controller!  I've looked at lots of documentation, but can't seem to find what I"m looking for.

Basically what this controller does is supports the VF page below which dynamically adds or deletes rows in order to add multiple opportunities at a time.

I know how to write a test case for something like the save method, but I need help when it comes to page references and other methods like addrows or deleterows.  

Thank you so much for any help!!

User-added image

public class ManageListController
{
public List<OpportunityWrapper> wrappers {get; set;}
public static Integer toDelIdent {get; set;}
public static Integer addCount {get; set;}
private Integer nextIdent=0;
 
public ManageListController()
{
  wrappers=new List<OpportunityWrapper>();
  for (Integer idx=0; idx<5; idx++)
  {
   wrappers.add(new OpportunityWrapper(nextIdent++));
  }
}
 
public void delWrapper()
{
  Integer toDelPos=-1;
  for (Integer idx=0; idx<wrappers.size(); idx++)
  {
   if (wrappers[idx].ident==toDelIdent)
   {
    toDelPos=idx;
   }
  }
  
  if (-1!=toDelPos)
  {
   wrappers.remove(toDelPos);
  }
}
 
public void addRows()
{
  for (Integer idx=0; idx<addCount; idx++)
  {
   wrappers.add(new OpportunityWrapper(nextIdent++));
  }
}
 
public PageReference save()
{
  List<Opportunity> opps=new List<Opportunity>();
  for (OpportunityWrapper wrap : wrappers)
  {
   opps.add(wrap.opp);
  }
  
  insert opps;
  
  return new PageReference('/' + Schema.getGlobalDescribe().get('Opportunity').getDescribe().getKeyPrefix() + '/o');
}
 
public class OpportunityWrapper
{
  public Opportunity opp {get; private set;}
  public Integer ident {get; private set;}
  
  public OpportunityWrapper(Integer inIdent)
  {
   ident=inIdent;
   opp=new Opportunity(Name='Cash Donation',RecordTypeId='012i0000000Hrcv',StageName='Posted',Probability=100);
  }
}
}
Best Answer chosen by rivereridanus
Puja_mfsiPuja_mfsi
Hi,
You need to cover all the above code in test class.First you need to make an object of your class as:
ManageListController obj = new ManageListController();

the above line cover class constructor.In constructor u have take the reference of wrapper class so the above line automatically cover the wrapper class also. After that you need to call the other method with the class object

obj.delWrapper();
obj.addRows();
obj.save();

To cover all the line of the above methods you need to use some logic like for addRow() you need to give some value to "addCount" variable.Because this variable is static so u need use class name to access this variable so the whole test class is:


@isTest
private class TestManageListController {
    static testMethod void unitTest() {
        ManageListController obj = new ManageListController();
        ManageListController.toDelIdent = 2;
        obj.delWrapper();

        ManageListController.addCount= 2;
        obj.addRows();

        for (ManageListController.OpportunityWrapper wrap : obj.wrappers)  {
            wrap.opp.closedate = System.today();
        }
        obj.save();

    }
}

before calling save method you need to put value in close date because the close date is mandatory field in opprtunity.

Please let me know if u have some query on same.

All Answers

Puja_mfsiPuja_mfsi
Hi,
You need to cover all the above code in test class.First you need to make an object of your class as:
ManageListController obj = new ManageListController();

the above line cover class constructor.In constructor u have take the reference of wrapper class so the above line automatically cover the wrapper class also. After that you need to call the other method with the class object

obj.delWrapper();
obj.addRows();
obj.save();

To cover all the line of the above methods you need to use some logic like for addRow() you need to give some value to "addCount" variable.Because this variable is static so u need use class name to access this variable so the whole test class is:


@isTest
private class TestManageListController {
    static testMethod void unitTest() {
        ManageListController obj = new ManageListController();
        ManageListController.toDelIdent = 2;
        obj.delWrapper();

        ManageListController.addCount= 2;
        obj.addRows();

        for (ManageListController.OpportunityWrapper wrap : obj.wrappers)  {
            wrap.opp.closedate = System.today();
        }
        obj.save();

    }
}

before calling save method you need to put value in close date because the close date is mandatory field in opprtunity.

Please let me know if u have some query on same.

This was selected as the best answer
Sonam_SFDCSonam_SFDC
Hi,

Did you get a chance to go through this doc which gives you a custom code to write your test class for custom controller:
http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm
Looking at your code I think you will have to instantiate the controller and create a object of the Wrapper class and test the logic of the code you have in the Controller.

Would help if you could share your test code so  this team can help debug in case there are any misses in the test class..
rivereridanusrivereridanus
Thank you both so much! I was able to use exactly the code above.
SFDCDevQASFDCDevQA
Did this test class cover your wrapper and page as well or did you have to add to it or create additional test classes?
Thanks!
Amanda