我需要測試合同義務程序。
我沒有看到任何直接測試這種方法的方法。這違反了Single Responsibility Principle,只是做了太多事情。
我會ロ知道,如何安全地提取到新的方法或類下列職責:按單責任原則重構方法
public void askUserPathAndWord() {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(System.in));
String path;
String whatFind;
BlockingQueue<File> queue = new LinkedBlockingQueue<File>();
try {
System.out.println("Please, enter a Path and Word"
+ "(which you want to find):");
System.out.println("Please enter a Path:");
path = bufferedReader.readLine();
System.out.println("Please enter a Word:");
whatFind = bufferedReader.readLine();
if (path != null && whatFind != null) {
File endOfWorkFile = new File("GameOver.tmp");
CountDownLatch latch = new CountDownLatch(2);
FolderScan folderScan = new FolderScan(path, queue, latch,
endOfWorkFile);
FileScan fileScan = new FileScan(whatFind, queue, latch,
endOfWorkFile);
Executor executor = Executors.newCachedThreadPool();
executor.execute(folderScan);
executor.execute(fileScan);
latch.await();
System.out.println("Thank you!");
} else {
System.out.println("You did not enter anything");
}
} catch (IOException | RuntimeException e) {
System.out.println("Wrong input!");
e.printStackTrace();
} catch (InterruptedException e) {
System.out.println("Interrupted.");
e.printStackTrace();
}
}
Qustions:
我們如何能夠做到安全接下來的步驟:
- 問用戶的詞和路徑;給定一個單詞,使用正確的參數(Factory)創建一個FileScan ;
- 給定路徑,使用正確的參數(Factory)創建一個 FolderScan;
- 給定兩個 可運行和一個鎖存器,創建一個ExecutorService,將它們排隊,並在鎖存器上等待 。
如何通過'resolve(Path ..)'正確檢查用戶的路徑輸入genal?並且會證明程序不會「倒下」。 – 2013-02-27 19:50:50
我不確定這個問題是否正確... – 2013-02-27 20:11:27
要確定重構是正確的,你應該有自動測試(或者進行手動測試),它覆蓋了方法功能的100%。在現實世界中,您將測試一些功能(最常見的和一些邊緣情況)。那樣可以讓你確信重構是正確的。但是,你必須測試它。 – 2013-02-28 08:21:13