【发布时间】:2016-01-20 03:26:09
【问题描述】:
我是一名较新的 Java 开发人员。 但是我对编写Junit测试用例知之甚少。 我很快就要参加工作考试了。 他们希望我为此编写一个程序
- 要从任何网站读取 HTML,请说“http://www.google.com”(您 可以使用 Java 中内置 API 的任何 API,例如 URLConnection )
- 在控制台打印来自上述 url 的 HTML 并将其保存到一个文件 ( web-content.txt) 在本地机器中。
- 以上的 JUnit 测试用例 计划。
我已经完成了如下前两个步骤:
import java.io.*;
import java.net.*;
public class JavaSourceViewer{
public static void main (String[] args) throws IOException{
System.out.print("Enter url of local for viewing html source code: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String url = br.readLine();
try {
URL u = new URL(url);
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
int code = uc.getResponseCode();
String response = uc.getResponseMessage();
System.out.println("HTTP/1.x " + code + " " + response);
InputStream in = new BufferedInputStream(uc.getInputStream());
Reader r = new InputStreamReader(in);
int c;
FileOutputStream fout=new FileOutputStream("D://web-content.txt");
while((c = r.read()) != -1){
System.out.print((char)c);
fout.write(c);
}
fout.close();
} catch(MalformedURLException ex) {
System.err.println(url + " is not a valid URL.");
} catch (IOException ie) {
System.out.println("Input/Output Error: " + ie.getMessage());
}
}
}
现在我需要第三步的帮助。
【问题讨论】:
-
Google
JUnit tutorial看看你能找到什么。 -
您首先需要将您的方法分解为可测试的更小方法。
-
到目前为止,我已经在 Google 上搜索了 junit 教程,了解了一些关于如何为基本程序(如加法/减法)编写 junit 测试用例的知识。我仍然找不到太多帮助来为我的这个程序编写 junit 测试用例。 @mauhiz:你能告诉我应该打破和测试哪种方法以及如何测试?
-
@Ryan Malhotra ,您走在正确的轨道上,始终尝试遵循编程中的功能块,这将有助于很多场景,所以现在如果您这样做,那将是非常简单的一个做junits测试用例
-
. Still i cant find much help to write junit test cases。问问自己:这个功能之后我应该期待什么?我不应该期待什么?我可以对不同场景下每个函数的所有可能输入进行分组吗?