是否值得嘗試單元測試控制檯應用程序的主要方法?我應該單元測試一種主要方法嗎?
主要方法是不具有測試覆蓋率,現在唯一的代碼。
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Pass the root directory and the directory to scan.");
System.exit(1);
}
Path root = Paths.get(args[0]);
Path scan = Paths.get(args[1]);
Demo demo = new Demo();
String output = demo.scan(root, scan);
System.out.println(output);
}
沒有任何模擬依賴的能力(特別是'Demo#scan(Path,Path)'的結果,這段代碼並不是那種可測試的,這裏最有意義的單元測試就是以驗證不愉快的行爲(錯誤的參數,可能是不可靠的路徑)否則,你只是測試你的整個應用程序 – CollinD
@CollinD OP可以使用[PowerMockito](https://github.com/powermock/powermock/wiki/) mockitousage#how-to-mock-construction-of-new-objects)來模擬Demo類。 –