I read this two guides on writing unit test: First Guide and Second Guide. However they both dealt with classes that had methods or triggers. All my Apex class does is send data to a VF page. So I am not sure if my plan to test this class will work...

This is my Apex code:

public class FirstProjectController {
public Case myCase {get; set;}

//Lists used to transfer data into the VF page
public List<Case> cases {get; set;} 

//Lists that initialize the query   
//Query Obtains the Accounts with open cases 
public List<Case> queryOne = [SELECT AccountId, Status, Subject FROM Case WHERE IsClosed = False ORDER BY AccountId DESC];   

public FirstProjectController(ApexPages.StandardController stdController){        
    this.myCase = (Case)stdController.getRecord();   

    //Attempt to transfer data into the VF page
    try {
        this.cases = queryOne;                  
    //Catch possible error and throw an exception
    catch(Exception e){
        System.debug('Failed to display Tables');

}    }

This is my VF page:

<apex:page standardController="Case" extensions="FirstProjectController"> 

<apex:pageBlock title="Table of Accounts with Open Cases">
    <apex:pageBlockTable value="{!Cases}" var="c">
        <apex:column value="{!c.AccountId}"/>
        <apex:column value="{!c.Status}"/>
        <apex:column value="{!c.Subject}"/>
    </apex:pageBlockTable>   </apex:pageBlock>   </apex:page>

This is my plan of action for the unit test class:

  1. Create a new Case
  2. Insert the Case
  3. Run the same Query as in my apex class and store it in a list of Cases
  4. Do System.AssertEquals and check to see if the new case is in the list of Cases

Is my plan of action a good way of testing my apex class? Or is there a better way to test my apex class?

Update: Hey guys this is my testClass. My code coverage is 100% but my test failed...

public class FirstProjectControllerTest {
public static testMethod void testMyController() {    
    //Create a new Case
    Case caseOne = new Case(Subject='Testing 1');

    //Insert the new Case into Cases
    insert caseOne;

    //Check that caseOne is not null

    //Instantiate the extension and controller
    ApexPages.StandardController ctrl = new ApexPages.StandardController(caseOne);
    FirstProjectController ext = new FirstProjectController(ctrl);

    //Run the first query from the FirstProject Class
    List<Case> queryOne = [SELECT AccountId, Status, Subject FROM Case WHERE Id =:caseOne.Id];

    //Check that caseOne has the correct subject
    System.assertEquals('Testing 1', queryOne[0].Subject);

    //Check that caseOne is a new Case
    System.assertEquals('new', caseOne.Status);

}    }
  • That try/catch is completely unnecessary. You will need to actually construct your extension, which is not listed out in your steps.
    – Adrian Larson
    Aug 31, 2017 at 16:34
  • Hey @AdrianLarson thanks for the reply, I remove the try/catch. But what exactly do you mean by creating an extension on my steps?
    – Frankg26
    Aug 31, 2017 at 16:44
  • When you create your test, you need to instantiate the the controller and then your extension. Your extension being the class you have showed us above.
    – JRiffe
    Aug 31, 2017 at 16:47
  • Hey @JRiffe. I think I know how to instantiate the controller, it should be similar to Java. It is just creating a FirstProjectController object by calling the constructor, am I right? But how would I instantiate the extension? And by extension do you mean the VF page?
    – Frankg26
    Aug 31, 2017 at 16:56
  • Where is your test failing? Is it on your last assertion System.assertEquals('new', caseOne.Status);? Typically, you would only make assertions to verify the functionality of the specific piece of code that you're testing. You aren't setting Case.Status in the setup for your test, nor are you modifying it in your extension's constructor, so I'd probably recommend removing that assertion from your test (unless one of your requirements is that the Case attached to the controller should be updated to Status = new, in which case you should modify your code so it passes your test).
    – Derek F
    Aug 31, 2017 at 18:16

1 Answer 1


You pass the controller you instantiate into the instantiation of your extension.. Which is your your extension constructor is defined.

ApexPages.StandardController ctrl = new ApexPages.StandardController(record);
YourControllerExt ext = new YourControllerExt(ctrl);

In your case, 'record' would be the variable for the case object you created in your test class. You would still need an assertion as well. By perhaps asserting that your case variable is not null or something to that effect.

Also, as Derek F suggested, take a look at the Testing Custom Controllers and Constroller Extensions documentation in the Visualforce Developer Guide.

  • 2
    You beat me to it. About the only thing that I'd add is that there is documentation on Testing Custom Controllers and Constroller Extensions in the Visualforce developer's guide (as opposed to being in the Apex developer's guide, where most of the rest of unit testing documentation is).
    – Derek F
    Aug 31, 2017 at 17:05
  • BTW would you recommend to insert the Case before or after instantiating the controller/extension?
    – Frankg26
    Aug 31, 2017 at 17:05
  • I'll add that to my answer!
    – JRiffe
    Aug 31, 2017 at 17:05
  • @Frankg26 I would actually recommend inserting it in an @testSetup method.
    – JRiffe
    Aug 31, 2017 at 17:06
  • 1
    You're probably right there. You've got my +1!
    – Adrian Larson
    Aug 31, 2017 at 17:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .