[英]How can I test whether a for loop is available in the Class?
讓我先解釋一下用例。
用例是學習java
的學習者遵循一組書面指令並使用自動化測試(閱讀JUnit
),我想測試他們是否成功實施了指令。
例如,我需要以編程方式找出的事情是代碼中是否存在for
循環。 考慮以下代碼
class AClass {
public static void main(String[] args){
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}
}
}
有沒有辦法使用反射 API,我可以斷言AClass
包含for(int i=1; i<11; i++)
? 還是有其他方式?
這個問題是一個例子。 理想情況下,可能有諸如if (x) {}
類的代碼,我可能想在類中找到它。
我不確定這有多可行。
謝謝
反射顯然在這里無濟於事,因為它不允許訪問方法的內部結構(例如for
、 if
等指令)。
從問題來看,您似乎正在開發某種學習系統,該系統可以對學生的工作進行自動評估/評分
所以在我看來,你有兩個選擇:
for
循環的存在可能是源代碼分析器的一項任務,甚至可以在 Junit 之前運行。 一種額外的步驟,將檢查學生沒有作弊:// OK - this will pass
public void methodToImplement() {
for(int i = 0; i < 3; i++) {
System.out.println("Hello: " + i);
}
}
// this is an attempt to cheat that should be recognized:
public void methodToImplement() {
System.out.println("Hello: " + 0);
System.out.println("Hello: " + 1);
System.out.println("Hello: " + 2);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.