function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AlanisticAlanistic 

Basic SOQL Test Class

I have written my first (very basic) custom controller:
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];
    } 
     
}

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. 

 
AlanisticAlanistic
I've updated with the following:
 
static testMethod void myTest() {
        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?
Hargobind_SinghHargobind_Singh
You can use Assert to query and check against the size of lt and nlt. More information here: https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
AlanisticAlanistic
Okay, I'm almost there.  However, my assert always fails so I don't think my test record is being created.  Can you advise what I've missed?  I've tried the assert two ways to see if it made a difference or not.  My test code is highlighted.


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);
     } 
}
Hargobind_SinghHargobind_Singh
Hi, 

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. 
AlanisticAlanistic
I've updated to the below but I'm still getting the error "System.AssertException: Assertion Failed: Same value: 0" on running the test.  My updated test is below.  I just want to 

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);
     }