JUnitフレームワークでテストカバレッジを実行する必要があります。
JUnit-Tutorialを読みましたが、この知識を私の例に結び付けることはできません。
ファイルから読み取ったメソッドを単独でテストする方法は理解していますが、パスとaskUserPathAndWordをテストしてこれを完全にテストする方法がわかりません。どうすればこれについて良いテストを行うことができますか?
package task;
import java.io.*;
class SearchPhrase {
public void walk(String path, String whatFind) throws IOException {
File root = new File(path);
File[] list = root.listFiles();
for (File titleName : list) {
if (titleName.isDirectory()) {
walk(titleName.getAbsolutePath(), whatFind);
} else {
if (read(titleName.getAbsolutePath()).contains(whatFind)) {
System.out.println("File:" + titleName.getAbsolutePath());
}
}
}
}
// Read file as one line
public static String read(String fileName) {
StringBuilder strBuider = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new FileReader(new File(
fileName)));
String strInput;
while ((strInput = in.readLine()) != null) {
strBuider.append(strInput);
strBuider.append("\n");
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return strBuider.toString();
}
public void askUserPathAndWord() {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(System.in));
String path, whatFind;
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) {
walk(path, whatFind);
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();
}
}
public static void main(String[] args) {
SearchPhrase example = new SearchPhrase();
example.askUserPathAndWord();
}
}
次の質問:
- 統合の依存関係を完全にテストし、パスを確認するにはどうすればよいですか?
- どのポイントが良い、当然のことながらjunitテストを行う必要がありますか?
- この状況で失敗テストを使用する必要がありますか?
- どの最大パーセントプログラムをカバーできますか?
- プライベートメソッドと保護されたメソッドを(一般的に)テストする必要がありますか?