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
ranjan nagaraju 7ranjan nagaraju 7 

public static controller variables not visible in test classes

public with sharing class CustomObjectFactory
{
public class UserPrefsDefaults
  {
    public final String name = 'User Preferences';
    public final String bestBetsView = 'MBB';
    public final Integer maxPages = 5;
    public final Integer timeFrameDays = 3;
    public final Boolean showWhatIsNew = true;
  }
  
  public static UserPrefsDefaults userPrefsDefaults
  {
get{
  if (userPrefsDefaults==null){
  UserPrefsDefaults userPrefsDefaultsobj= new UserPrefsDefaults();
    //return userPrefsDefaultsobj;
  }
  return userPrefsDefaults;
  }

}}

on saving below test class im facing error 

@isTest
private class TestCustomObjectFactory 
{
    static testMethod void testUserPrefsGetter()
    {
        System.assertNotEquals(null, CustomObjectFactory.userPrefsDefaults);
        
        // test initialization
        CustomObjectFactory.userPrefsDefaults = null;
        System.assertNotEquals(null, CustomObjectFactory.userPrefsDefaults);
    }
    
    }

 
Best Answer chosen by ranjan nagaraju 7
Narender Singh(Nads)Narender Singh(Nads)
Hi Ranjan,
You are getting this error because you are trying to set the variable without defining set property. (IN LINE 34)

To resolve it use this code:
public with sharing class CustomObjectFactory
{
public class UserPrefsDefaults
  {
    public final String name = 'User Preferences';
    public final String bestBetsView = 'MBB';
    public final Integer maxPages = 5;
    public final Integer timeFrameDays = 3;
    public final Boolean showWhatIsNew = true;
  }
  
  public static UserPrefsDefaults userPrefsDefaults
  {
get{
  if (userPrefsDefaults==null){
  UserPrefsDefaults userPrefsDefaultsobj= new UserPrefsDefaults();
    //return userPrefsDefaultsobj;
  }
  return userPrefsDefaults;
  }
  set;
}}

Note: I would suggest not to use same class name for your object variable.

thanks!

All Answers

ranjan nagaraju 7ranjan nagaraju 7

Compile Error: Variable is not visible: ranjan234.CustomObjectFactory.userPrefsDefaults at line 34 column 29

 
Narender Singh(Nads)Narender Singh(Nads)
Hi Ranjan,
You are getting this error because you are trying to set the variable without defining set property. (IN LINE 34)

To resolve it use this code:
public with sharing class CustomObjectFactory
{
public class UserPrefsDefaults
  {
    public final String name = 'User Preferences';
    public final String bestBetsView = 'MBB';
    public final Integer maxPages = 5;
    public final Integer timeFrameDays = 3;
    public final Boolean showWhatIsNew = true;
  }
  
  public static UserPrefsDefaults userPrefsDefaults
  {
get{
  if (userPrefsDefaults==null){
  UserPrefsDefaults userPrefsDefaultsobj= new UserPrefsDefaults();
    //return userPrefsDefaultsobj;
  }
  return userPrefsDefaults;
  }
  set;
}}

Note: I would suggest not to use same class name for your object variable.

thanks!
This was selected as the best answer
ranjan nagaraju 7ranjan nagaraju 7
setter is not mandatory on salesforce API version 20.0, i was not getting this error with 20.0 im getting this error on upgrading to 42.0
Narender Singh(Nads)Narender Singh(Nads)
Sorry, I have no clue about the API version 20.0.