You need to sign in to do that
Don't have an account?

Test class not inserting test data (System.AssertException: Assertion Failed)
I've written a test class for a custom object but the class fails with the error System.AssertException: Assertion Failed. I'm guessing this is because the test data is not being written.
My full class and testing class is below. Can anyone advise what I've missed?
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();
// Insert with duration
Live_Training__c testSession1 = new Live_Training__c(
Title__c = 'Test Session',
Description__c = 'Testing',
Duration__c = 40,
Training_Date__c = Date.today());
insert testSession1;
// Insert without duration
Live_Training__c testSession2 = new Live_Training__c(
Title__c = 'Test Session',
Description__c = 'Testing',
Training_Date__c = Date.today());
insert testSession2;
System.assert(ctrLT.lt.size() >0);
System.assert(ctrLT.nlt.size() >0);
}
}
My full class and testing class is below. Can anyone advise what I've missed?
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();
// Insert with duration
Live_Training__c testSession1 = new Live_Training__c(
Title__c = 'Test Session',
Description__c = 'Testing',
Duration__c = 40,
Training_Date__c = Date.today());
insert testSession1;
// Insert without duration
Live_Training__c testSession2 = new Live_Training__c(
Title__c = 'Test Session',
Description__c = 'Testing',
Training_Date__c = Date.today());
insert testSession2;
System.assert(ctrLT.lt.size() >0);
System.assert(ctrLT.nlt.size() >0);
}
}
If you have any queries in your constructor (or whatever sets the lt variable), the data will not exist yet and you'll have an empty list of Live_Training__c records. If you move the inserts above your constructor call, then it should fix the problem.
Furthermore, it's a best practice to create a separate test class and move your test methods out of any non test classes.