[英]Testing a method that is called from another method
我有一個logon
方法,該方法調用addUser
,同一類中的另一個方法。
我們讓HS學生編寫各種方法,並希望他們能夠獨立測試每種方法。 我們有一個JAR文件,其中沒有完整編碼的所有方法(PROD類)的源代碼,並且學生在DEV類中使用相同的方法。
有沒有一種方法可以將addUser
的DEV副本與PROD方法addUser
使用,以測試應用程序是否可以實現其addUser
的實現?
您可以嘗試子類化,盡管它依賴PROD類是友好的(允許覆蓋其方法)。
class DEV extends PROD {
@Override
int addUser(String name) {
... students work and test this one implementation
}
public static void main(String[] args) {
... do my testing thing here, like for instance
System.out.println("This is the expected result using PROD:");
PROD prod = new PROD();
int result = prod.logon(); // internally calls PROD.logon() and PROD.addUser()
System.out.println(result);
System.out.println("This is the test result using DEV:");
DEV dev = new DEV();
result = dev.logon(); // internally calls PROD.logon() and DEV.addUser()
System.out.println(result);
}
}
這將如下工作:
編譯: javac -classpath PROD.jar DEV.java
要測試: java -classpath .;PROD.jar DEV
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.