当需要测试这个程序时,应该使用哪种方式更好。
首先askUserPathAndWord()
要求用户输入path
和whatFind
。我们有两个线程:
- 第一个线程扫描文件夹,如果找到可读文件
put()
,则将其放入队列中。 take()
队列中的第二个线程s 并whatFind
在此文件中查找。如果搜索成功,它将输出到控制台该文件的路径和单词的频率。
这是与多线程工作的集成依赖。哪个变体能够更好地测试这个程序 - EasyMock 的 Junit?我阅读了一些关于 EasyMock 的教程,但我不知道在哪些情况下使用它更好。
代码:
class FolderScan implements Runnable {
private String path;
private BlockingQueue<File> queue;
private CountDownLatch latch;
private File endOfWorkFile;
FolderScan(String path, BlockingQueue<File> queue, CountDownLatch latch,
File endOfWorkFile) {
this.path = path;
this.queue = queue;
this.latch = latch;
this.endOfWorkFile = endOfWorkFile;
}
public FolderScan() {
}
@Override
public void run() {
findFiles(path);
queue.add(endOfWorkFile);
latch.countDown();
}
private void findFiles(String path) {
try {
File root = new File(path);
File[] list = root.listFiles();
for (File currentFile : list) {
String s = currentFile.getName().toLowerCase();
if (currentFile.isDirectory()) {
findFiles(currentFile.getAbsolutePath());
} else {
if (s.matches("^.*?\\.(txt|pdf|doc|docx|html|htm|xml|djvu|rar|rtf)$")) {
queue.put(currentFile);
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class FileScan implements Runnable {
private String whatFind;
private BlockingQueue<File> queue;
private CountDownLatch latch;
private File endOfWorkFile;
public FileScan(String whatFind, BlockingQueue<File> queue,
CountDownLatch latch, File endOfWorkFile) {
this.whatFind = whatFind;
this.queue = queue;
this.latch = latch;
this.endOfWorkFile = endOfWorkFile;
}
public FileScan() {
}
@Override
public void run() {
while (true) {
try {
File file;
file = queue.take();
if (file == endOfWorkFile) {
break;
}
scan(file);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
latch.countDown();
}
private void scan(File file) {
Scanner scanner = null;
int matches = 0;
try {
scanner = new Scanner(file);
} catch (FileNotFoundException e) {
System.out.println("File Not Found.");
e.printStackTrace();
}
while (scanner.hasNext())
if (scanner.next().equals(whatFind)) {
matches++;
}
if (matches > 0) {
String myStr = String.format(
"File: %s - and the number of matches " + "is: %d",
file.getAbsolutePath(), matches);
System.out.println(myStr);
}
}
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();
}
}
public static void main(String[] args) {
new FileScan().askUserPathAndWord();
}
}
问题:
- 在这种情况下,哪种测试的覆盖率最好?
- 如何更好地测试两种变体中的合同义务?
- 如果答案是
EasyMock
,我们应该如何正确地做到这一点? - 如果
Junit
,我们如何测试void
方法?