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:
- Create a new Case
- Insert the Case
- Run the same Query as in my apex class and store it in a list of Cases
- 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);
} }
try
/catch
is completely unnecessary. You will need to actually construct your extension, which is not listed out in your steps.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 settingCase.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 theCase
attached to the controller should be updated toStatus = new
, in which case you should modify your code so it passes your test).