Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a class which contains only public static final strings how do i write a test class to cover this code?

public class SFA_MobileConstants {

 public static final string ERROR='ERROR';
 public static final string FAIL='FAIL';
 public static final string SUCCESS='SUCCESS';
 public static final string RECSAVEMSG='Record saved';


public class testconstants {

     public testMethod static void testSFA_MobileConstants(){


share|improve this question

You should find that if you are testing the code that references these strings they should be covered. Otherwise add a test method to the class that references those strings.

public with sharing class TestStrings {

    public static final String TEST = 'MyString';

    public static void testStrings()
         System.assertEquals(TEST, TEST);

If your variables and tests are in different classes, just be sure to qualify the class name.

public with sharing class TestStrings2 {

    public static void testStrings()
        System.assertEquals(TestString.TEST, TestString.TEST);

In this case you really are just gaining coverage, as we expect the platform behaviour to work and of course provide consistently the strings we assign. So while you could do the same without the assert, it just feels wrong from a best practice point of view. And of course would raise the eye of the security scanner.

enter image description here

share|improve this answer
I tried but i cannot assign again values as its a final variable and simply assigning these into another variable not helping either – Mohith Shrivastava Dec 17 '12 at 9:16
Ok I assumed the strings are defined as in my answer, perhaps if you can publish a sample in your answer I can better understand what you mean. – Andrew Fawcett Dec 17 '12 at 9:17
Mine is in separate class and the test code is in different class – Mohith Shrivastava Dec 17 '12 at 9:26
Andrew thanks for reply.I have given the code in my question – Mohith Shrivastava Dec 17 '12 at 9:28
No problem, I've updated my answer to show a test from another class referencing your static final variables from another. – Andrew Fawcett Dec 17 '12 at 9:31

Please add a constructor like this:

public SFA_MobileConstants() {}

It will fix your issues.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.