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

Apex Novice - Need Help Finishing Test Class

I'm just getting started with Apex and am having a little trouble getting my first test class past 57%. The test is for a very simple custom controller for a visualforce page which returns a list of values from an SOQL query, then updates any records that are modified. The controller works fine, and I've poured over all the developer documentation and board posts I can find but still can't get the test to fly.


I intend to improve my Apex knowledgebase with some additional training but in the meantime really need to get this one class up and running. Any help would be greatly appreciated. My current code is below:


Custom Controller:

public class plantListingClass {
    public Account[] accts = new Account[0];

  public Account[] getAccts() {
        accts = [SELECT ID, Parent.Name, Name, BillingState, Logo__c,  
        Category__c, Report_Classification__c, Region__c, Comments__c, Status_Update__c, 
        FROM Account 
        WHERE Report_Classification__c = 'Existing Plant' 
        ORDER BY Region__c DESC, Logo__c, Parent.Name];
        RETURN accts;       

public PageReference save() {
     UPDATE accts;
     return null;

 Visualforce Page:

<apex:page controller="plantListingClass" tabstyle="account" sidebar="false">
   <font size="6">
   Activity Report: Existing Plants
    <BR /> <BR />      

   <apex:form >
   <apex:pageBlock >
   <apex:pageMessages />
   <apex:pageBlockSection title="Existing Plants"/>
   <apex:pageBlockTable value="{!accts}" var="a">
      <apex:column value="{!a.Region__c}"/> 

      <apex:column headerValue="Parent">
         <apex:outputField value="{!a.Logo__c}"/>

      <apex:column headerValue="Company">
         <apex:outputField value="{!a.Parent.Name}"/>

      <apex:column headerValue="Plant">
         <apex:outputField value="{!a.Name}"/>
      <apex:column headerValue="State">
         <apex:outputField value="{!a.BillingState}"/>
      <apex:column value="{!a.Last_Update__c}"/>                 
      <apex:column headerValue="Status Update">
         <apex:inputField value="{!a.Status_Update__c}"/>
      <apex:column headerValue="Comments">
         <apex:inputField value="{!a.Comments__c}"/>
  <apex:pageBlockButtons >
      <apex:commandButton value="Save" action="{!save}"/>
Report Generated: <apex:outputText value="{!NOW()}"></apex:outputText>    

 Test Class:

private class plantListingTest {
    static testMethod void validatePlantListingClass() {
     Account testAccount = new Account(Name='Test Company Name',
                                        Category__c = 'Customer / Prospect',
                                        Report_Classification__c = 'Existing Plant',
                                        Region__c = 'West Region',
                                        Comments__c = 'Test Comments',
                                        Status_Update__c = 'Pending Bid',
     insert testAccount;
     update testAccount;

     // Verify that the billingState field was updated in the database.
     Account updatedAccount = [SELECT billingState FROM Account WHERE Id = :testAccount.Id];
     System.assertEquals('CA', updatedAccount.billingState);
        plantListingClass ctrl = new plantListingClass();
        Integer accSize = ctrl.getAccts().size();
        System.assert (accSize >= 1);



 Here is my code coverage report:



What you are doing does not actually require a custom controller, SF can do all that out of the box. However if you are just testing/playing around then ignore what I said.


To get code coverage you need to call the functions defined in your controller ("getAccts()", "save()") and possibly test each account retrieved is correct.


Thanks for the clarification about the function calls, SimonJai. I'll keep working on it.


It's my understanding that, in order to filter record results with an SOQL query, it is necessary to use a custom controller. Originally, I tried setting up the page with a Standard Account Controller and a controller extension to handle the SOQL query, but wasn't able to get the "save" functionality to work on the page. That's how I wound up using a custom controller (which works fine, except for the testing trouble).



I think what you did initially was correct, using the standard controller and writing an extension. The reason is because using the standard controller a lot of the code has already been written for you, and you don't have the hassle of writing it again.

I would then rewrite the save function to do what you want it to.

That's my 2 cents. Good luck!