0

I am trying to write a test class for this but I keep getting 0 coverage.

global class Company_Global
{
    public static final List<organization> lstOrg  = [Select isSandbox from Organization];
    public static string Environment;

    public static string getEnvironment()
    {
        if(lstOrg[0].isSandbox == False){
            Environment = 'Production';
            return Environment;
        }else{
            Environment = UserInfo.getUserName().substringAfterLast('.').toUpperCase();
            return Environment;
        }
    }
}

Test Class

@isTest(seeAllData=true)
public class Company_Global_test
{
    public static final List<organization> lstOrg  = [Select isSandbox from Organization];
    public static string Environment;

    public static testmethod string getEnvironment()
    {
       if(lstOrg[0].isSandbox == False){
            Environment = 'Production';
            return Environment;
        }else{
            Environment = UserInfo.getUserName().substringAfterLast('.').toUpperCase();
            return Environment;
        }
    }    
}
2
  • 1
    You need to call -> Company_Global_test.getEnvironment(); then only the method can be covered.
    – Ysr Shk
    Commented Jun 18, 2018 at 13:58
  • 2
    Your test class is not really testing your Company_Global class. You need to invoke the methods in your Company_Global from the test class, but as of now you have just written the same code that you have in your class in the test class as well. Take a look at these resources to get started on how to write a unit test -- What are Apex Unit Tests? & Get Started with Apex Unit Tests
    – Jayant Das
    Commented Jun 18, 2018 at 13:59

1 Answer 1

3

The purpose of a test class is to call the code and verify its output. That means your test method should look like this:

@isTest static void testGetEnvironment() {
  String environment = Company_Global.getEnvironment();
  ...

However, to really get 100% coverage, you need to inject data into your test:

@isTest static void testGetEnvironment() {
  Organization prodBox = (Organization)JSON.deserialize('{"IsSandbox":false}', Organization.class),
    testBox = (Organization)JSON.deserialize('{"IsSandbox":true}', Organization.class);
  Company_Global.lstOrg[0] = prodBox;
  System.assertEquals('Production',Company_Global.getEnvironment());
  Company_Global.lstOrg[0] = testBox;
  System.assertEquals(UserInfo.getUserName().substringAfterLast('.').toUpperCase(),Company_Global.getEnvironment());
}      
6
  • +1 to injecting data to avoid (seeAllData=true)...!!
    – Ysr Shk
    Commented Jun 18, 2018 at 14:13
  • 2
    @YsrShk SeeAllData isn't meaningful to this test; you can always query Organization. However, the field "IsSandbox" is locked, so you have to inject it via JSON deserialization. Also, it makes sure you will have 100% coverage no matter which org you're in.
    – sfdcfox
    Commented Jun 18, 2018 at 14:36
  • Actually I did not know a way to avoid seeAllData for some instances, where we use the existing things. Hence I added it. Curious to know if it works for static resource and custom label.
    – Ysr Shk
    Commented Jun 18, 2018 at 14:38
  • I am not getting 100% coverage due to the asserEqual not handling 'Production" correctly so any thoughts?
    – Daryn
    Commented Jun 18, 2018 at 18:32
  • @Daryn Yeah, I accidentally reversed the "true" and "false" values in the test data. Edited my answer to reflect how it should look. This is the reason why you should always use asserts, to catch errors like this.
    – sfdcfox
    Commented Jun 18, 2018 at 18:34

You must log in to answer this question.

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