[SalesForce] Writing Unit Tests on an Apex Class that only has a constructor and no triggers

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…

@isTest 
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
    System.assertNotEquals(caseOne,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);

}    }

Best Answer

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.

Related Topic