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
tsalbtsalb 

Test Class - how to test (example from blog)

I'm having a hard time understanding how to initialize a (custom) standard controller in my test method for a example i've modified on a blog. This is borrowed from the blog: http://bobbuzzard.blogspot.co.uk/2011/07/managing-list-of-new-records-in.html.

 

Up until now, I've followed existing examples of test classes to "understand" my way around creating and testing standard controllers and extensions. Can anyone help me understand how to intialize the controller for my test class? For testing this kind of controller - do I need system assert or system assert equals?

 

I'm still trying to understand everything - slowly but surely - thanks for any help

 

Controller

 

public class ScorecardController {
	
 public List<VSCWrapper> wrappers {get; set;}
 public static Integer toDelIdent {get; set;}
 public static Integer addCount {get; set;}
 public String ParentId = ApexPages.currentPage().getParameters().get('ParentId');
 private Integer nextIdent=0;
  
 public ScorecardController() {
   wrappers=new List<VSCWrapper>();
   for (Integer idx=0; idx<1; idx++) {
     wrappers.add(new VSCWrapper(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 VSCWrapper(nextIdent++));
   }
 }
  
 public PageReference save() {
   List<Vendor_Scorecard__c> accs=new List<Vendor_Scorecard__c>();
   for (VSCWrapper wrap : wrappers) {
     accs.add(wrap.acc);
   }
   insert accs;
   return new PageReference('/' + ParentId);
 }
  
 public class VSCWrapper {
   public Vendor_Scorecard__c acc {get; private set;}
   public Integer ident {get; private set;}
   public String ParentId = ApexPages.currentPage().getParameters().get('ParentId');
  
   public VSCWrapper(Integer inIdent) {
     ident=inIdent;
     acc=new Vendor_Scorecard__c(Service_Request__c = ParentId);
   }
 }
 
//------------------------Begin Test Method------------------------

public static testMethod void testScorecardController() {
    
    //Create test Task Order
    Task_Order__c t = new Task_Order__c(Name = 'TestClassDemo');
    Insert t;	       
    
    //Create test Account
    Account a = new Account(Name = 'TestClassDemo', Task_Order__c = t.Id, Lin__c = '9999');
    Insert a;
   
    //Create test Contact
    Contact c = new Contact(LastName = 'TestClassDemo', AccountId = a.Id);
    Insert c; 
    
    //Create test Service Request
    Service_Request__c sr = new Service_Request__c(Task_Order__c = t.Id);
    Insert sr;
    
    Vendor_Scorecard__c vsc = new Vendor_Scorecard__c(Service_Request__c = sr.Id, Vendor__c = c.Id);
  	  	
    //Set the Test Page
    PageReference sc  = new PageReference('/apex/?'+sr.Id);
    Test.setCurrentPage(sc);
    
    ApexPages.StandardController se = new ApexPages.StandardController(vsc);   //PROBLEM LINE
       
    //Test Controller
    PageReference sP = se.save();
    
    //Test Controller Methods (need)
    se.ScorecardController();
    se.delWrapper();
    se.addRows();
    se.VSCWrapper();
   
        
}

//------------------------End Test Method------------------------    
 
 
}

 

 

Page

<apex:page controller="ScorecardController" tabstyle="Vendor_Scorecard__c">
 <apex:form style="width:40%">
   <apex:pageBlock title="Bulk Creation">
   <apex:outputText value="Additional items only - not clone(s) of existing"/>
      <apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">

         <apex:column headerValue="Service Request">
            <apex:inputField value="{!wrapper.acc.Service_Request__c}"/>
         </apex:column>
         <apex:column headerValue="Vendor Name">
            <apex:inputField value="{!wrapper.acc.Vendor__c}"/>
         </apex:column>
         <apex:column headerValue="Action">
            <apex:commandButton value="Delete" action="{!delWrapper}" rerender="wtable">
               <apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/> 
            </apex:commandButton>
         </apex:column>
      </apex:pageBlockTable>
      <apex:commandButton value="Add Row" action="{!addRows}" rerender="wtable">
         <apex:param name="addCount" value="1" assignTo="{!addCount}"/> 
      </apex:commandButton>

      <apex:commandButton value="Save" action="{!save}"/>
   </apex:pageBlock>
 </apex:form>
</apex:page>

 

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,


You have to simply create the instance of class inside the test method and call all the methods of class inside the test method by using the instance of that class. Crete unit test data inside the test method when ever needed. Try the below class with test method as reference:


public class ScorecardController
{

public List<VSCWrapper> wrappers {get; set;}
public static Integer toDelIdent {get; set;}
public static Integer addCount {get; set;}
public String ParentId = ApexPages.currentPage().getParameters().get('ParentId');
private Integer nextIdent=0;

public ScorecardController() {
wrappers=new List<VSCWrapper>();
for (Integer idx=0; idx<1; idx++) {
wrappers.add(new VSCWrapper(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 VSCWrapper(nextIdent++));
}
}

public PageReference save() {
List<Vendor_Scorecard__c> accs=new List<Vendor_Scorecard__c>();
for (VSCWrapper wrap : wrappers) {
accs.add(wrap.acc);
}
insert accs;
return new PageReference('/' + ParentId);
}

public class VSCWrapper {
public Vendor_Scorecard__c acc {get; private set;}
public Integer ident {get; private set;}
public String ParentId = ApexPages.currentPage().getParameters().get('ParentId');

public VSCWrapper(Integer inIdent) {
ident=inIdent;
acc=new Vendor_Scorecard__c(Service_Request__c = ParentId);
}
}
static testmethod void testAbove()
{
ScorecardController s=new ScorecardController();
s.delWrapper();
s.save();
s.addRows();
}
}

 

For more details about test method please go through the link below:

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.