Android JUnitTests-是否可以为方法和字段反射创建公共静态方法?

Android JUnitTests-是否可以为方法和字段反射创建公共静态方法?,android,unit-testing,reflection,private,static-methods,Android,Unit Testing,Reflection,Private,Static Methods,我知道如何从我的类在测试类中访问私有方法或字段: 从名为void doSomething()的MyClass访问私有方法: import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; ... try { Method method = MyClass.class.getDeclaredMethod("doSomething", (Class[])null); // (Class

我知道如何从我的
测试类
中访问
私有
方法或
字段

从名为
void doSomething()
MyClass
访问私有方法:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
...

try {
    Method method = MyClass.class.getDeclaredMethod("doSomething", (Class[])null); // (Class[])null is for parameterless methods
    method.setAccessible(true);
    method.invoke(localInstanceOfMyClass);
}
catch (NoSuchMethodException ex){
    ex.printStackTrace();
}
catch (IllegalAccessException ex){
    ex.printStackTrace();
}
catch (IllegalArgumentException ex){
    ex.printStackTrace();
}
catch (InvocationTargetException ex) {
    ex.printStackTrace();
}
import java.lang.reflect.Field;
...

try {
    Field field = MyClass.class.getDeclaredField("myField");
    field.setAccessible(true);
    field.set(localInstanceOfMyClass, true); // true is the value I want to assign to myField
}
catch (NoSuchFieldException ex){
    ex.printStackTrace();
}
catch (IllegalAccessException ex){
    ex.printStackTrace();
}
catch (IllegalArgumentException ex){
    ex.printStackTrace();
}
MyClass
访问名为
boolean myField
的私有字段:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
...

try {
    Method method = MyClass.class.getDeclaredMethod("doSomething", (Class[])null); // (Class[])null is for parameterless methods
    method.setAccessible(true);
    method.invoke(localInstanceOfMyClass);
}
catch (NoSuchMethodException ex){
    ex.printStackTrace();
}
catch (IllegalAccessException ex){
    ex.printStackTrace();
}
catch (IllegalArgumentException ex){
    ex.printStackTrace();
}
catch (InvocationTargetException ex) {
    ex.printStackTrace();
}
import java.lang.reflect.Field;
...

try {
    Field field = MyClass.class.getDeclaredField("myField");
    field.setAccessible(true);
    field.set(localInstanceOfMyClass, true); // true is the value I want to assign to myField
}
catch (NoSuchFieldException ex){
    ex.printStackTrace();
}
catch (IllegalAccessException ex){
    ex.printStackTrace();
}
catch (IllegalArgumentException ex){
    ex.printStackTrace();
}
(来源:)

如您所见,这是相当多的代码,用于将
私有布尔值
置于
true
false


所以,我的问题是:是否有可能以某种方式使两个
公共静态方法
,两个用于
方法
,一个用于
字段
,我可以在所有
测试类中使用它们?澄清:

TestMethodsClass.setPrivateField(... some parameters ...);
TestMethodsClass.runPrivateVoidMethod(... some parameters ...);
TestMethodsClass.runPrivateReturnMethod(... some parameters ...);
基于上述示例
void doSomething()
en
boolean myField
的参数示例:

TestMethodsClass.setPrivateField(localInstanceOfMyClass, "myField", true);
TestMethodsClass.runPrivateVoidMethod(localInstanceOfMyClass, "doSomething", null);
// PS: null will be converted in the method itself to (Class[])null`

提前感谢您的回复。

好的,其实很简单

测试方法类:

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestMethodsClass
{
    public static void runPrivateVoidMethod(Object ob, String methodName, Class<?>[] parameters){
        try {
            Method method = null;
            if(parameters == null){
                Class<?>[] nullParameter = (Class[])null;
                method = ob.getClass().getDeclaredMethod(methodName, nullParameter);
            }
            else
                method = ob.getClass().getDeclaredMethod(methodName, parameters);

            if(method != null){
                method.setAccessible(true);
                method.invoke(ob);
            }
        }
        catch (NoSuchMethodException ex){
            ex.printStackTrace();
        }
        catch (IllegalAccessException ex){
            ex.printStackTrace();
        }
        catch (IllegalArgumentException ex){
            ex.printStackTrace();
        }
        catch (InvocationTargetException ex) {
            ex.printStackTrace();
        }
    }

    public static Object runPrivateReturnMethod(Object ob, String methodName, Class<?>[] parameters){
        Object returnObject = null;
        try {
            if(parameters == null){
                Class<?>[] nullParameter = (Class[])null;
                Method method = ob.getClass().getDeclaredMethod(methodName, nullParameter);
                method.setAccessible(true);
                returnObject = method.invoke(ob);
            }
            else{
                Method method = ob.getClass().getDeclaredMethod(methodName, parameters);
                method.setAccessible(true);
                method.invoke(ob);
            }
        }
        catch (NoSuchMethodException ex){
            ex.printStackTrace();
        }
        catch (IllegalAccessException ex){
            ex.printStackTrace();
        }
        catch (IllegalArgumentException ex){
            ex.printStackTrace();
        }
        catch (InvocationTargetException ex) {
            ex.printStackTrace();
        }
        return returnObject;
    }

    public static void setPrivateField(Object ob, String fieldName, Object value){
        try {
            Field field = ob.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(ob, value);
        }
        catch (NoSuchFieldException ex){
            ex.printStackTrace();
        }
        catch (IllegalAccessException ex){
            ex.printStackTrace();
        }
        catch (IllegalArgumentException ex){
            ex.printStackTrace();
        }
    }
}
我还没有完全测试它,所以我不知道它是否适用于所有类型。但据我所知,弦乐和布尔音效果相当不错。稍后我会做更多的测试,当我有一些空闲时间从我的项目