[英]Does ReflectionTestUtils works only on fields of a class, not on variables defined inside a method of that class?
Does ReflectionTestUtils works only on fields of a class, not on variables defined inside a method of that class? ReflectionTestUtils是否仅适用于类的字段,而不适用于该类的方法内部定义的变量?
I tried to test the fields of a class, it works perfectly fine using ReflectionTestUtils
, if I try it on variables of a method, I get an IllegalArgumentException
. 我尝试测试一个类的字段,使用
ReflectionTestUtils
可以很好地工作,如果在方法的变量上进行尝试,则会得到IllegalArgumentException
。
Example Code: 示例代码:
public class Check {
String city;
public void method() {
city = "Bangalore";
String Street = "MGRoad";
}
}
If I want to have a JUnit tests for the above class using ReflectionTestUtils
. 如果我想使用
ReflectionTestUtils
对上述类进行JUnit测试。
Check c = new Check();
assert ReflectionTestUtils.getField(c, "city").equals("Bangalore")
-> works fine.
assert ReflectionTestUtils.getField(c, "Street").equals("MGRoad")
-> gives illegalArgumentException.
Please have a look and suggest me if I can test the attributes of a method. 如果我可以测试方法的属性,请看看并建议我。
You cannot access local variables using reflection and String Street = "MGRoad";
您不能使用反射和
String Street = "MGRoad";
访问局部变量String Street = "MGRoad";
is local variable to that method 是该方法的局部变量
If what you mean is variables local to methods/constructors, you can not access them with reflection.
如果您的意思是方法/构造函数本地的变量,则不能通过反射访问它们。 ... There is no way to obtain this information via reflection.
...无法通过反射获取此信息。 Reflection works on method level, while local variables are on code block level.
反射在方法级别起作用,而局部变量在代码块级别起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.