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
Andy Morton 14Andy Morton 14 

Create Apex Test for checking integers - controller extension.

Hello

I've put together an account controller extension for a Visualforce report page I'm working. The extension returns a number of integers from count queries on case objects:
 
public with sharing class CSRCntrlExt1 {
    
    Private final Account acct;
    
    // Set Public Variables
    
    Public Integer traisedlast30 {get; set;}
    Public Integer tclosedlast30 {get; set;}
    Public Integer tp1last30 {get; set;}
    Public Integer tp2last30 {get; set;}
    Public Integer tp3last30 {get; set;}
    Public Integer tp4last30 {get; set;}
    Public Integer traisedlast180 {get; set;}
    Public Integer tflexlast30 {get; set;}
    Public Integer tcasplast30 {get; set;}
    Public Integer tmanagedlast30 {get; set;}
       
        
    //Get Account Record
    
		public CSRCntrlExt1(ApexPages.StandardController stdController) {
        this.acct = (Account)stdController.getRecord();    

    //Get Number of tickets raised in last 30 days
    traisedlast30 = [Select count() FROM Case WHERE AccountId =:acct.Id AND CreatedDate <= LAST_N_DAYS:30];
            
    //Get Number of tickets closed in last 30 days
    tclosedlast30 = [Select count() FROM Case WHERE AccountId =:acct.Id AND status='Closed' AND ClosedDate <= LAST_N_DAYS:30];
            
    //Get Number of P1 tickets raised in last 30 days
    tp1last30 =  [Select count() FROM Case WHERE AccountId =:acct.Id AND CreatedDate <= LAST_N_DAYS:30 AND Priority = 'P1'];       
            
    //Get Number of P2 tickets raised in last 30 days
    tp1last30 =  [Select count() FROM Case WHERE AccountId =:acct.Id AND CreatedDate <= LAST_N_DAYS:30 AND Priority = 'P2']; 
            
    //Get Number of P3 tickets raised in last 30 days
    tp1last30 =  [Select count() FROM Case WHERE AccountId =:acct.Id AND CreatedDate <= LAST_N_DAYS:30 AND Priority = 'P3']; 
            
    //Get Number of P4 tickets raised in last 30 days
    tp1last30 =  [Select count() FROM Case WHERE AccountId =:acct.Id AND CreatedDate <= LAST_N_DAYS:30 AND Priority = 'P4']; 
    
    //Get Number of tickets raised in last 6 months
    traisedlast180 = [Select count() FROM Case WHERE AccountId =:acct.Id AND CreatedDate <= LAST_N_DAYS:180];
            
    //Get Number of Flexsupport tickets raised in last 30 days
    tflexlast30 = [Select count() FROM Case WHERE AccountId=:acct.id AND CreatedDate <= LAST_N_DAYS:30 AND Type = 'Flexsupport'];
            
    //Get Number of CASP tickets raised in last 30 days
    tcasplast30 = [Select count() FROM Case WHERE AccountId=:acct.id AND CreatedDate <= LAST_N_DAYS:30 AND Type = 'CASP'];
    
    //Get Number of Managed Services tickets raised in last 30 days
    tmanagedlast30 = [Select count() FROM Case WHERE AccountId=:acct.id AND CreatedDate <= LAST_N_DAYS:30 AND Type = 'Managed Services']; 
            
            
        }    
            
}

I'm trying to put together a test class for this to allow me to upload it into production but really struggling with knowing where to start. So far I have:
 
@isTest
private class testCSRCntrlExt1 {
   
    static testMethod void Test1(){
        
        //Create Account
        Account acc = new Account(name='testAccount');
        insert acc;
        
        //Create Contact
        Contact con = new Contact(lastname='Test', Accountid=acc.id);
        insert con;
        
        //Create Case
        Case cas = new Case(accountid=acc.id, type='Flexsupport', Priority='P3', contactid=con.id, subject='Test', Status='new');
        insert cas;     
        
        //Create the Controller
        ApexPages.StandardController accountController = new ApexPages.StandardController(acc);
        
        //Create the instance of the Controller
        CSRCntrlExt1 ext1 = new CSRCntrlExt1(accountController);
        
        
        
    }  
   
}

Not even sure what I have is correct (still trying to get my head around Apex coding).

Anyone able to assist in creating a test class for this? I think all that's needed is assertion of values returned, but not sure on the syntax for that.

Any help appreciated as always. 

Andy
Best Answer chosen by Andy Morton 14
Andy Morton 14Andy Morton 14
Ok, managed to solve with the following (after I realised I was using the same variable for four of the SQL queries in the extension):
 
@isTest
private class testCSRCntrlExt1 {
    
    
   @isTest static void Test1setup(){
        
                     
        //Create Account
        Account acc = new Account(name='testAccount');
        insert acc;
        
        //Create Contact
        Contact con = new Contact(lastname='Test', FirstName='Ted', Accountid=acc.id);
        insert con;
        
        //Create Case
        Case cas = new Case(accountid=acc.id, type='Flexsupport', Priority='P3', contactid=con.id, subject='Test', Status='new');
        insert cas;     
  

        //Create the Controller
        ApexPages.StandardController accountController = new ApexPages.StandardController(acc);
        
        //Create the instance of the Controller
        CSRCntrlExt1 ext1 = new CSRCntrlExt1(accountController);         
                
       Integer rlast30 = ext1.traisedlast30;
       System.assertEquals(1, rlast30);
       
       Integer clast30 = ext1.tclosedlast30;
       System.assertEquals(0, clast30);
       
       Integer p1last30 = ext1.tp1last30;
       System.assertEquals(0, p1last30);
       
       Integer p2last30 = ext1.tp2last30;
       System.assertEquals(0, p2last30);
       
       Integer p3last30 = ext1.tp3last30;
       System.assertEquals(1, p3last30);
       
       Integer p4last30 = ext1.tp4last30;
       System.assertEquals(0, p4last30);
       
       Integer rlast180 = ext1.traisedlast180;
       System.assertEquals(1, rlast180);
       
       Integer flexlast30 = ext1.tflexlast30;
       System.assertEquals(1, flexlast30);
       
       Integer casplast30 = ext1.tcasplast30;
       System.assertEquals(0, casplast30);
       
       Integer mslast30 = ext1.tmanagedlast30;
       System.assertEquals(0, mslast30);
       
       
        
        
        
    }
   
}
100% coverage achieved. Appreciate any feedback/suggestions on improvement/best practice on this (still learning so constructive feedback is always appreciated).