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
JoshTonksJoshTonks 

test class for a record count

Hi,

I am trying o do a record count on a specific criteria which i have working. However the issue I have is I dont know how to write a test class for it. Ive mainly worked with lists up until now which i can write a test class for. I have attached the apex class below would someone be able to give me some direction on what i need to put in the test class.
 
public class UnitCount {

    public Integer getC2630Counter(){
    	return [SELECT count()
                FROM Unit_Value__c
                WHERE Unit_Type__c =: 'LMU-2630' AND Stock_Status__c =: 'OFFICE' AND Account__c = '0010E00000DHfef' AND Status__c =: 'New Stock'
                ];
	}
}

 
Best Answer chosen by JoshTonks
sfdcMonkey.comsfdcMonkey.com
Hi Tonks, 
use below test class for 100% coverge :
@isTest 
public class UnitCountTest {
    
     // create test data here for Unit_Value__c object if you want some record count,
     //  however this test class will give you 100% coverge 
    static testMethod void testMethod1(){
        UnitCount oUnitCount = new UnitCount();
        Integer rowCount = oUnitCount.getC2630Counter();
        
    }
}

additional note : and try to avoid hard coded account id in SOQL query, which is not best coding practice.

Thanks, let us know if it helps you 
 

All Answers

Deepak Maheshwari 7Deepak Maheshwari 7

Hi,

Please let me know from where this class is called.

sfdcMonkey.comsfdcMonkey.com
Hi Tonks, 
use below test class for 100% coverge :
@isTest 
public class UnitCountTest {
    
     // create test data here for Unit_Value__c object if you want some record count,
     //  however this test class will give you 100% coverge 
    static testMethod void testMethod1(){
        UnitCount oUnitCount = new UnitCount();
        Integer rowCount = oUnitCount.getC2630Counter();
        
    }
}

additional note : and try to avoid hard coded account id in SOQL query, which is not best coding practice.

Thanks, let us know if it helps you 
 
This was selected as the best answer
JoshTonksJoshTonks
Thanks piyush_soni you are a legend. Thanks for the advice on the hard coded account id.