0

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

2 Answers 2

2

Four points I'd like to make out here.

Debug Expected First

System.assertEquals(ApexConstantsCls.INITIAL_SUBMIT,'Draft');

This is backwards and will give you nonsensical errors if this changes. Put the expected value first.

System.assertEquals('Draft',ApexConstantsCls.INITIAL_SUBMIT);

Performance Warning

We had a class like this once, and it grew to approximately 300 constants. It added approximately 1 second to every request when any constant was used. The fix is simple; use a custom getter and no setter:

public static Integer DEFAULT_SIZE { get { return 3; } }

This has zero overhead cost if the variable is never accessed, so it performs a lot better.

Testing Everything

You just need to continue testing each line:

System.assertEquals('Draft',ApexConstantsCls.INITIAL_SUBMIT);
System.assertEquals(3,ApexConstantsCls.DEFAULT_SIZE);
System.assertEquals('An error has occurred, please contact Admin.',ApexConstantsCls.ERROR_MESSAGE);

Use Custom Labels When Possible

User-facing values should use Custom Labels. This allows you to translate the error into other languages later, if you end up needing to do so. You also get to avoid having to write unit tests, and you can change the message without a full deployment.

0

To cover all three line you need to call all three variables in the test method. Because whenever you call the Property at that time property line will cover.

   @istest public class testtt {
        static TestMethod void Product2Extension_UnitTest1(){
            Test.startTest();
            System.assertEquals(ApexConstantsCls.DEFAULT_SIZE,3);
            System.assertEquals(ApexConstantsCls.INITIAL_SUBMIT,'Draft');
            System.assertEquals(ApexConstantsCls.ERROR_MESSAGE,'An error has occurred, please contact Admin.');
            test.stopTest();
        }
    }
3
  • I tried this but unfortunately it didn cover :(
    – Quddus
    Commented Aug 22, 2018 at 12:13
  • It will cover but sometime on class it will show in RED. You can compare number of lines and covered lines. Commented Aug 22, 2018 at 12:38
  • Actually, it didn't while checking the number of lines as well, and the code coverage also didn't increase. I finally used the static block to get it covered
    – Quddus
    Commented Aug 23, 2018 at 9:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .