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()
enboolean 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();
}
}
}
我还没有完全测试它,所以我不知道它是否适用于所有类型。但据我所知,弦乐和布尔音效果相当不错。稍后我会做更多的测试,当我有一些空闲时间从我的项目