You need to sign in to do that
Don't have an account?
Andy 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:
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:
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
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
100% coverage achieved. Appreciate any feedback/suggestions on improvement/best practice on this (still learning so constructive feedback is always appreciated).