我有從網站獲取HTML的Java程序。它在控制檯上顯示內容,然後將其保存到名爲web_content.txt
的文件中。我如何爲此編寫測試用例?Junit用於創建文件的void方法的測試案例
我的計劃是:
public class UrlDown {
public static void main(String[] args) throws Exception {
UrlDown down = new UrlDown();
File f = new File("web_content.txt");
String loc = "http://www.google.com";
down.saveUrlToFile(f, loc);
}
public void saveUrlToFile(File saveFile, String location) {
URL url;
try {
url = new URL(location);
BufferedReader in = new BufferedReader(new InputStreamReader(
url.openStream()));
BufferedWriter out = new BufferedWriter(new FileWriter(saveFile));
char[] cbuf = new char[255];
StringBuilder builder = new StringBuilder();
while ((in.read(cbuf)) != -1) {
out.write(cbuf);
builder.append(cbuf);
}
String downloaded = builder.toString();
System.out.println();
System.out.println(downloaded);
in.close();
out.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
請告訴我泰德問題?在您提供的代碼中沒有關於Junit的內容。 – Tom 2012-04-26 14:39:25