You need to sign in to do that
Don't have an account?
Francisco Garcia 34
My unit test is failing! Help?
Hey guys I am completed a controller extension that sends data to a VF page and then the VF page displays it as a table.
Now, I wrote a unit test class for the controller extension. It has 100% coverage but it is failing unfortunately...
This is my VF page:
This is my Apex code:
This is my actual test code:
Any form of help would be greatly appreciated!
Now, I wrote a unit test class for the controller extension. It has 100% coverage but it is failing unfortunately...
This is my VF page:
<apex:page standardController="Case" extensions="FirstProjectController"> <!-- Accounts with Open Cases --> <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> <!-- Accounts with No Cases --> <apex:pageBlock title="Table of Accounts with no Cases"> <apex:pageBlockTable value="{!Accounts}" var="a"> <apex:column value="{!a.Name}"/> </apex:pageBlockTable> </apex:pageBlock> <!-- Five most Recent Cases --> <apex:pageBlock title="Table of the Five most Recent Cases"> <apex:pageBlockTable value="{!cases2}" var="c"> <apex:column value="{!c.Subject}"/> <apex:column value="{!c.CreatedDate}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:page>
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;} public List<Account> accounts {get; set;} public List<Case> cases2 {get; set;} //Lists that initialize the querys //Query Obtains the Accounts with open cases public List<Case> queryOne = [SELECT AccountId, Status, Subject FROM Case WHERE IsClosed = False ORDER BY AccountId DESC]; //Query Obtains the Accounts with no cases public List<Account> queryTwo = [Select Name FROM Account WHERE Id NOT IN (SELECT AccountId FROM Case) ORDER BY Name ASC]; //Query Obtains the 5 most recent cases public List<Case> queryThree = [SELECT Subject, CreatedDate FROM Case ORDER BY CreatedDate DESC Limit 5]; public FirstProjectController(ApexPages.StandardController stdController){ this.myCase = (Case)stdController.getRecord(); //Transfer the data into the VF page this.cases = queryOne; this.accounts = queryTwo; this.cases2 = queryThree; } }
This is my actual test code:
@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', caseOne.Subject); //Check that caseOne is a new Case System.assertEquals('new', caseOne.Status); } }
Any form of help would be greatly appreciated!
100% test coverage and zero failure with just this little modification (null instead of 'new')
With the developer console, you can see the tab "Test" and know your exact error for your tests (clicking and double clicking on the result) Class variable: it is correct (syntax) to have an initialized value directly (here it is the [SELECT ...]) even it is rarely written like that for lists but that works.
public List<Case> queryOne = [SELECT AccountId, Status, Subject FROM Case WHERE IsClosed = False ORDER BY AccountId DESC];
[public | private | protected | global] [final] [static] ***data_type*** variable_name [= value]
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_declaring_variables.htm
So you have a 100% test coverage and just a little error for your first training test.
Regards
All Answers
I went over lots of documentation for the controller and I know the controller has no issue (It does what it is supposed to). As for the testing I went over this three sources:
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm
https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
Do you have another source for testing? Specifically trailhead source, because the three I went over were not particularly helpful.
Thanks for the reply
1. Your test is not testing your controller extension. It instanciates it but your asserts just check the case you create in the test class. You still do not say what is failing but it should be your second assert.
2. Your queries should not be placed where they are but should just be in the constructor or an init method.
100% test coverage and zero failure with just this little modification (null instead of 'new')
With the developer console, you can see the tab "Test" and know your exact error for your tests (clicking and double clicking on the result) Class variable: it is correct (syntax) to have an initialized value directly (here it is the [SELECT ...]) even it is rarely written like that for lists but that works.
public List<Case> queryOne = [SELECT AccountId, Status, Subject FROM Case WHERE IsClosed = False ORDER BY AccountId DESC];
[public | private | protected | global] [final] [static] ***data_type*** variable_name [= value]
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_declaring_variables.htm
So you have a 100% test coverage and just a little error for your first training test.
Regards
The fact that somthing works does not make it right, there is no use in initialize a member directly to only assign it to antoher member in the constructor.
Same goes for your updated test. It has 100% coverage and the asserts pass, but the controller is not tested and the assert do not have any meaning, they assert data only created for the test purpose and absolutely no code outside the testclass.
People should not be thaugt to write something that compiles, they should learn to create something correctly and more important understand what the difference is.
The question is "My unit test is failing! Help?"
"the controller is not tested"? ... 100% test coverage nevertheless.
This VFP has no button (nor navigation) and that works. Francisco has seen the results by himself (I am not the author of the code but that works).
Post your best class of test as you imagine, please. You provide no solution until now. Franciso is interesting by any solution for the test class here.
It is a minimal training class which works perfectly (no buttons, no navigation) and the syntax is correct according the compiler.
It is only the assertions which can be better perhaps but there is no buttons nor navigations.
Regards
If the goal is to get coverage you do not understand what you are doing, I do not want to give Francisco a working assert if the assert is not what he should be doing.
People should not be given a working solution they should get the information to be able to come to the correct solution themselves.
To actually test this controller extension what should be done is that the test data created should contain accounts with open cases, accounts with closed cases and accounts without cases with the total amount of cases larger then 5.
Then the Extension should be instatiated. (This will give the coverage)
And finally asserts should be created to check:
1. if the member cases contains all accounts with open cases (it actually lists cases and not accounts so a count will not work)
2. if the member accounts contains all accounts without cases
3. if cases2 contains the 5 most recent cases.
A test class for a controller extension should allways test the functionality in the extension.
I am leaving LDV problems out here, which should also be taken into consideration.
The solution for replacing the status with null implies that the case status is not 'New', while the actual status in the database is 'New' it just is not queried in the Test. Even removing the assert would have been a better, but stil very wrong solution to the problem.