You need to sign in to do that
Don't have an account?
Alanistic
Basic SOQL Test Class
I have written my first (very basic) custom controller:
Could anyone provide me an example of a test class? I've reviewed the documentation on Salesforce but there doesn't seem to be anything specific to checking a simple SOQL query.
public with sharing class ctr_LiveTraining {
public List<Live_Training__c> lt { get; set;}
public List<Live_Training__c> nlt { get; set;}
public ctr_LiveTraining() {
getTraining();
getNextSession();
}
public List<Live_Training__c> getTraining(){
lt = [select Id, Name, Training_Date__c, Duration__c, Description__c
from Live_Training__c where duration__c != null
order by Training_Date__c DESC LIMIT 25];
return lt;
}
public void getNextSession(){
nlt=[select Name, Training_Date__c, Description__c, Registration_Link__c
from Live_Training__c
where Duration__c = null
order by Training_Date__c DESC LIMIT 1];
}
}
public List<Live_Training__c> lt { get; set;}
public List<Live_Training__c> nlt { get; set;}
public ctr_LiveTraining() {
getTraining();
getNextSession();
}
public List<Live_Training__c> getTraining(){
lt = [select Id, Name, Training_Date__c, Duration__c, Description__c
from Live_Training__c where duration__c != null
order by Training_Date__c DESC LIMIT 25];
return lt;
}
public void getNextSession(){
nlt=[select Name, Training_Date__c, Description__c, Registration_Link__c
from Live_Training__c
where Duration__c = null
order by Training_Date__c DESC LIMIT 1];
}
}
Could anyone provide me an example of a test class? I've reviewed the documentation on Salesforce but there doesn't seem to be anything specific to checking a simple SOQL query.
Live_Training__c ltx = new Live_Training__c(
Title__c = 'Test Session',
Description__c = 'Testing');
insert ltx;
}
How do I amend the above to check if the lists lt and nlt are greater than 0?
public with sharing class ctr_LiveTraining {
public List<Live_Training__c> lt { get; set;}
public List<Live_Training__c> nlt { get; set;}
public ctr_LiveTraining() {
getTraining();
getNextSession();
}
public List<Live_Training__c> getTraining(){
lt = [select Id, Name, Training_Date__c, Duration__c, Description__c
from Live_Training__c where duration__c != null
order by Training_Date__c DESC LIMIT 25];
return lt;
}
public void getNextSession(){
nlt=[select Name, Training_Date__c, Description__c, Registration_Link__c
from Live_Training__c
where Duration__c = null
order by Training_Date__c DESC LIMIT 1];
}
private static testMethod void test_ctr_LiveTraining() {
ctr_LiveTraining ctrLT = new ctr_LiveTraining();
Live_Training__c ltx = new Live_Training__c(
Title__c = 'Test Session',
Description__c = 'Testing',
Training_Date__c = Date.today());
insert ltx;
System.assertNotEquals(ctrLT.lt.size(),0);
System.assert(ctrLT.nlt.size() >0);
}
}
Both your methods getTraining, and getNextSession have different where conditions. So you would need two records, one that satisfies where condition of one method and other that satisfies for other. Which means you would need to have two Inserts, and make sure that you fill the correct fields.
Plus, since both your methods have different where conditions, you can check Assert with one query only, which means in your test class, you need to query twice and then compare.
hope this makes sense.
private static testMethod void test_ctr_LiveTraining() {
ctr_LiveTraining ctrLT = new ctr_LiveTraining();
Live_Training__c ltx1 = new Live_Training__c(
Title__c = 'Test Session',
Description__c = 'Testing',
Duration__c = 40,
Training_Date__c = Date.today());
insert ltx1;
Live_Training__c ltx2 = new Live_Training__c(
Title__c = 'Test Session',
Description__c = 'Testing',
Training_Date__c = Date.today());
insert ltx2;
System.assertNotEquals(ctrLT.lt.size(),0);
System.assert(ctrLT.nlt.size() >0);
}