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
Quddus Ololade 4Quddus Ololade 4 

Test Class Cover for Constant Class in Salesforce

Below is the sample code :
public class ApexConstantsCls { 
        public static final INTEGER DEFAULT_SIZE= 3; 
        public static final STRING INITIAL_SUBMIT= 'Draft';
        public static final STRING ERROR_MESSAGE = 'An error has occurred, please contact Admin.';
    }

I tried with assert and defining a constructor as well but it didnt work. The first line which is an integer is getting covered though but not the other lines
Test Class:
 
@istest public class testtt {
    static TestMethod void Product2Extension_UnitTest1(){
        Test.startTest();
        System.assertEquals(ApexConstantsCls.INITIAL_SUBMIT,'Draft');
        test.stopTest();
    }
}

Any help appreciated.
Raj VakatiRaj Vakati
try this 
 
@istest public class testtt {
    static TestMethod void Product2Extension_UnitTest1(){
        Test.startTest();
	Integer val = ApexConstantsCls.INITIAL_SUBMIT ; 
	String val2 = ApexConstantsCls.DEFAULT_SIZE ; 
	String val4 = ApexConstantsCls.ERROR_MESSAGE  ; 
	
        System.assertEquals(val,'Draft');
        test.stopTest();
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8

I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm


Sample Test Class for you
Try below code
 
@istest public class testtt {
    static TestMethod void Product2Extension_UnitTest1(){
        Test.startTest();
			System.assertEquals(ApexConstantsCls.INITIAL_SUBMIT,'Draft');
			System.assertEquals(ApexConstantsCls.INTEGER DEFAULT_SIZE,3);
			System.assertEquals(ApexConstantsCls.INITIAL_SUBMIT,'Draft');
			System.assertEquals(ApexConstantsCls.STRING ERROR_MESSAGE ,'An error has occurred, please contact Admin.');
		test.stopTest();
    }
}

Let us know if this will help you