私有静态字段的设定值


问题内容

我想使用反射对单元测试设置私有字段的值。

问题是,该字段是静态的。

这是我正在从事的工作:

/**
   * Use to set the value of a field you don't have access to, using reflection, for unit testing.
   * 
   * Returns true/false for success/failure.
   * 
   * @param p_instance an object to set a private field on
   * @param p_fieldName the name of the field to set
   * @param p_fieldValue the value to set the field to
   * @return true/false for success/failure
   */
  public static boolean setPrivateField(final Object p_instance, final String p_fieldName, final Object p_fieldValue) {
    if (null == p_instance)
      throw new NullPointerException("p_instance can't be null!");
    if (null == p_fieldName)
      throw new NullPointerException("p_fieldName can't be null!");

    boolean result = true;

    Class<?> klass = p_instance.getClass();

    Field field = null;
    try {
      field = klass.getDeclaredField(p_fieldName);

      field.setAccessible(true);
      field.set(p_instance, p_fieldValue);

    } catch (SecurityException e) {
      result = false;
    } catch (NoSuchFieldException e) {
      result = false;
    } catch (IllegalArgumentException e) {
      result = false;
    } catch (IllegalAccessException e) {
      result = false;
    }

    return result;
  }

我意识到这可能已经在SO上得到了解答,但是我的搜索并没有找到答案…


问题答案:

基本上,问题是您的实用程序方法,该方法假设您有一个实例。设置私有静态字段相当容易-
与实例字段完全相同,但您指定null为实例除外。不幸的是,您的实用程序方法使用实例来获取类,并要求该类为非null …

我会回应汤姆的警告:不要那样做。如果这是您可以控制的类,那么我将创建一个包级方法:

void setFooForTesting(Bar newValue)
{
    foo = newValue;
}

但是,如果您 确实 想要通过反射进行设置 ,那么 这里有一个完整的示例:

import java.lang.reflect.*;

class FieldContainer
{
    private static String woot;

    public static void showWoot()
    {
        System.out.println(woot);
    }
}

public class Test
{
    // Declared to throw Exception just for the sake of brevity here
    public static void main(String[] args) throws Exception
    {
        Field field = FieldContainer.class.getDeclaredField("woot");
        field.setAccessible(true);
        field.set(null, "New value");
        FieldContainer.showWoot();
    }
}