You need to sign in to do that
Don't have an account?
Oleg Nikitchuk
Struggling with my test assignment
Hey guys and girls,
I am still struggling with my test class. I have written a test class for my custom controller. Let me share the code first.
VF page:
<apex:page controller="MyPositionsController" showHeader="true" sidebar="true">
<apex:form>
<apex:pageBlock title="Positions list">
<apex:outputLabel value="View: "></apex:outputLabel>
<apex:selectList value="{!filterId}" size="1">
<apex:actionSupport event="onchange" action="{!processRequests}" rerender="positions_table"/>
<apex:selectOptions value="{!items}"/>
</apex:selectList>
<apex:pageBlockTable value="{!positions}" var="pos" rows="20" id="positions_table">
<apex:column value="{!pos.Name}"></apex:column>
<apex:column headerValue="Status">
<apex:inputField value="{!pos.Status__c}"></apex:inputField>
</apex:column>
<apex:column value="{!pos.Opening_date__c}"></apex:column>
<apex:column value="{!pos.Closing_date__c}"></apex:column>
<apex:column value="{!pos.Salary_max__c}"></apex:column>
<apex:column value="{!pos.Salary_min__c}"></apex:column> -->
</apex:pageBlockTable>
<apex:pageMessages id="showmsg"></apex:pageMessages>
<apex:pageBlockButtons location="top">
<apex:commandButton value="Save" action="{!save}" reRender="showmsg"></apex:commandButton>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex custom controller:
public with sharing class MyPositionsController {
public List<Position__c> positions {get;set;}
public String filterId {get;set;}
public MyPositionsController(){
init();
}
public void init(){
positions = [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c FROM Position__c ORDER BY Status__c LIMIT 25];
}
public void processRequests(){
if (filterId == 'All') {
positions = [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c FROM Position__c WHERE Status__c != null ORDER BY Name LIMIT 25];
} else{
positions = [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c FROM Position__c WHERE Status__c = :filterId ORDER BY Name LIMIT 25];
}
}
public List<SelectOption> getItems(){
List<SelectOption> options = new List<SelectOption>();
Schema.DescribeFieldResult fieldResult = Position__c.Status__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
options.add(new SelectOption('All','All'));
for (Schema.PicklistEntry f : ple) {
options.add(new SelectOption(f.getLabel(),f.getValue()));
}
return options;
}
public void save(){
update positions;
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM,'Successfully saved!');
ApexPages.addMessage(myMsg);
init();
}
}
Test class:
@isTest
public with sharing class MyPositionsController_tests {
@testSetup
static void setup(){
Position__c pos = new Position__c();
pos.Status__c = 'Status';
pos.Opening_date__c = Date.today();
pos.Closing_date__c = Date.today().addDays(30);
pos.Salary_max__c = 5000;
pos.Salary_min__c = 500;
Position__c pos1 = new Position__c();
pos1.Status__c = 'Status';
pos1.Opening_date__c = Date.today();
pos.Closing_date__c = Date.today().addDays(60);
pos.Salary_max__c = 10000;
pos.Salary_min__c = 300;
insert pos;
insert pos1;
}
@isTest static void testMyPositionsController(){
Position__c pos = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1];
Test.startTest();
MyPositionsController posController = new MyPositionsController();
Test.stopTest();
System.assertEquals('Status', pos.Status__c);
}
@isTest static void testProcessRequestsOpenPositions(){
/* Position__c pos1 = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1];
Position__c pos2 = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1]; */
Test.startTest();
MyPositionsController posController = new MyPositionsController();
posController.filterId = 'Opened';
posController.processRequests();
Test.stopTest();
System.assertEquals('Opened', posController.filterId);
}
/* @isTest static void testProcessRequestsOpenPositions(){
Position__c pos = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1];
Test.startTest();
MyPositionsController posController = new MyPositionsController();
posController.processRequests();
Test.stopTest();
System.assertEquals(null, posController.filterId);
} */
@isTest static void testGetItems(){
Position__c pos = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1];
Test.startTest();
MyPositionsController posController = new MyPositionsController();
posController.getItems();
Test.stopTest();
System.assertEquals('Status', pos.Status__c);
}
@isTest static void testSaveButton(){
Test.startTest();
MyPositionsController posController = new MyPositionsController();
posController.save();
Test.stopTest();
}
}
-------------------------------------------------------------------------------------------------
So in my test class, it seems like I am not testing the correct data. For example:
1) testMyPositionsController() - here I created a controller, but it seems that i am not testing it. i am checking the position that was created earlier, but instead, i needto check if that position was written in the controller field. I have a List of positions in my controller, I have to check if my position is in that list.
2) testGetItems() - seems like again I am not testing it correctly. I am testing the position.My method returns a list of select options, so i guess i need to test this method of select options. I don't know how to do that.
3) testSaveButton() - same here, wrong test logic. I have a controller, which has a List with positions. i guess i should change some fields in these positions and then call save() method. then i need to get my positions from DB and check of DB updated. Also need to test if myssage was shown after the save() button.
Please advice!
I am still struggling with my test class. I have written a test class for my custom controller. Let me share the code first.
VF page:
<apex:page controller="MyPositionsController" showHeader="true" sidebar="true">
<apex:form>
<apex:pageBlock title="Positions list">
<apex:outputLabel value="View: "></apex:outputLabel>
<apex:selectList value="{!filterId}" size="1">
<apex:actionSupport event="onchange" action="{!processRequests}" rerender="positions_table"/>
<apex:selectOptions value="{!items}"/>
</apex:selectList>
<apex:pageBlockTable value="{!positions}" var="pos" rows="20" id="positions_table">
<apex:column value="{!pos.Name}"></apex:column>
<apex:column headerValue="Status">
<apex:inputField value="{!pos.Status__c}"></apex:inputField>
</apex:column>
<apex:column value="{!pos.Opening_date__c}"></apex:column>
<apex:column value="{!pos.Closing_date__c}"></apex:column>
<apex:column value="{!pos.Salary_max__c}"></apex:column>
<apex:column value="{!pos.Salary_min__c}"></apex:column> -->
</apex:pageBlockTable>
<apex:pageMessages id="showmsg"></apex:pageMessages>
<apex:pageBlockButtons location="top">
<apex:commandButton value="Save" action="{!save}" reRender="showmsg"></apex:commandButton>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex custom controller:
public with sharing class MyPositionsController {
public List<Position__c> positions {get;set;}
public String filterId {get;set;}
public MyPositionsController(){
init();
}
public void init(){
positions = [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c FROM Position__c ORDER BY Status__c LIMIT 25];
}
public void processRequests(){
if (filterId == 'All') {
positions = [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c FROM Position__c WHERE Status__c != null ORDER BY Name LIMIT 25];
} else{
positions = [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c FROM Position__c WHERE Status__c = :filterId ORDER BY Name LIMIT 25];
}
}
public List<SelectOption> getItems(){
List<SelectOption> options = new List<SelectOption>();
Schema.DescribeFieldResult fieldResult = Position__c.Status__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
options.add(new SelectOption('All','All'));
for (Schema.PicklistEntry f : ple) {
options.add(new SelectOption(f.getLabel(),f.getValue()));
}
return options;
}
public void save(){
update positions;
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM,'Successfully saved!');
ApexPages.addMessage(myMsg);
init();
}
}
Test class:
@isTest
public with sharing class MyPositionsController_tests {
@testSetup
static void setup(){
Position__c pos = new Position__c();
pos.Status__c = 'Status';
pos.Opening_date__c = Date.today();
pos.Closing_date__c = Date.today().addDays(30);
pos.Salary_max__c = 5000;
pos.Salary_min__c = 500;
Position__c pos1 = new Position__c();
pos1.Status__c = 'Status';
pos1.Opening_date__c = Date.today();
pos.Closing_date__c = Date.today().addDays(60);
pos.Salary_max__c = 10000;
pos.Salary_min__c = 300;
insert pos;
insert pos1;
}
@isTest static void testMyPositionsController(){
Position__c pos = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1];
Test.startTest();
MyPositionsController posController = new MyPositionsController();
Test.stopTest();
System.assertEquals('Status', pos.Status__c);
}
@isTest static void testProcessRequestsOpenPositions(){
/* Position__c pos1 = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1];
Position__c pos2 = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1]; */
Test.startTest();
MyPositionsController posController = new MyPositionsController();
posController.filterId = 'Opened';
posController.processRequests();
Test.stopTest();
System.assertEquals('Opened', posController.filterId);
}
/* @isTest static void testProcessRequestsOpenPositions(){
Position__c pos = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1];
Test.startTest();
MyPositionsController posController = new MyPositionsController();
posController.processRequests();
Test.stopTest();
System.assertEquals(null, posController.filterId);
} */
@isTest static void testGetItems(){
Position__c pos = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1];
Test.startTest();
MyPositionsController posController = new MyPositionsController();
posController.getItems();
Test.stopTest();
System.assertEquals('Status', pos.Status__c);
}
@isTest static void testSaveButton(){
Test.startTest();
MyPositionsController posController = new MyPositionsController();
posController.save();
Test.stopTest();
}
}
-------------------------------------------------------------------------------------------------
So in my test class, it seems like I am not testing the correct data. For example:
1) testMyPositionsController() - here I created a controller, but it seems that i am not testing it. i am checking the position that was created earlier, but instead, i needto check if that position was written in the controller field. I have a List of positions in my controller, I have to check if my position is in that list.
2) testGetItems() - seems like again I am not testing it correctly. I am testing the position.My method returns a list of select options, so i guess i need to test this method of select options. I don't know how to do that.
3) testSaveButton() - same here, wrong test logic. I have a controller, which has a List with positions. i guess i should change some fields in these positions and then call save() method. then i need to get my positions from DB and check of DB updated. Also need to test if myssage was shown after the save() button.
Please advice!
Oleg Nikitchuk
Somebody, help!