[英]Does ReflectionTestUtils works only on fields of a class, not on variables defined inside a method of that class?
ReflectionTestUtils是否僅適用於類的字段,而不適用於該類的方法內部定義的變量?
我嘗試測試一個類的字段,使用ReflectionTestUtils
可以很好地工作,如果在方法的變量上進行嘗試,則會得到IllegalArgumentException
。
示例代碼:
public class Check {
String city;
public void method() {
city = "Bangalore";
String Street = "MGRoad";
}
}
如果我想使用ReflectionTestUtils
對上述類進行JUnit測試。
Check c = new Check();
assert ReflectionTestUtils.getField(c, "city").equals("Bangalore")
-> works fine.
assert ReflectionTestUtils.getField(c, "Street").equals("MGRoad")
-> gives illegalArgumentException.
如果我可以測試方法的屬性,請看看並建議我。
您不能使用反射和String Street = "MGRoad";
訪問局部變量String Street = "MGRoad";
是該方法的局部變量
如果您的意思是方法/構造函數本地的變量,則不能通過反射訪問它們。 ...無法通過反射獲取此信息。 反射在方法級別起作用,而局部變量在代碼塊級別起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.