Find your content:

Search form

You are here

Code coverage for Classes with only final static string Variables

 
Share

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';

}


@istest
public class testconstants {

     public testMethod static void testSFA_MobileConstants(){

  System.assertEquals(SFA_MobileConstants.ERROR,SFA_MobileConstants.ERROR);     
  }

}

Attribution to: Mohith Shrivastava

Possible Suggestion/Solution #1

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';

    @IsTest
    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 {

    @IsTest
    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


Attribution to: Andrew Fawcett

Possible Suggestion/Solution #2

Please add a constructor like this:

public SFA_MobileConstants() {}

It will fix your issues.


Attribution to: BruceYue
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/5475

My Block Status

My Block Content