我需要测试合同义务程序。
我没有看到任何直接的方法来测试这种方法。它违反了单一职责原则,并且干了太多事情。
我会知道,如何安全地将以下职责提取到新方法或类中:
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();
}
}
问题:
我们如何能够安全地执行后续步骤:
- 向用户询问单词和路径;
- 给定一个词,创建一个具有正确参数的 FileScan(工厂);
- 给定一个路径,创建一个具有正确参数的 FolderScan(一个工厂);
- 给定两个runnables和一个latch,创建一个ExecutorService,将它们排队,然后等待latch。