Java 需要编写JUnit测试用例吗
我是一个新的java开发人员。 但是我对编写Junit测试用例没有太多的知识。 我很快就要参加工作考试了。 他们想让我写一个程序Java 需要编写JUnit测试用例吗,java,junit,Java,Junit,我是一个新的java开发人员。 但是我对编写Junit测试用例没有太多的知识。 我很快就要参加工作考试了。 他们想让我写一个程序 要从任何网站阅读HTML,请说“http://www.google.com“(你 可以在类似Java的URLConnection中使用内置API的任何API) 在控制台上打印上面url中的HTML并将其保存到文件中( 本地计算机中的web content.txt) 上面的JUnit测试用例 方案 我已完成以下前两个步骤: import java.io.*; impor
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());
}
}
}
现在我需要第三步的帮助。- 首先,将代码从主方法移动到 JavaSourceViewer李>
- 第二,让这些方法返回 你可以测试。e、 g.输出文件或读取器的文件名
import org.junit.* ;
import static org.junit.Assert.* ;
public class JavaSourceViewerTest {
@Test
public void testJavaSourceViewer() {
String url = "...";
JavaSourceViewer jsv = new JavaSourceViewer();
// call you methods here to parse the site
jsv.xxxMethod(...)
....
// call you checks here like:
// <file name to save to output data from your code, actual filename> - e.g.
assertEquals(jsv.getOutputFile(), "D://web-content.txt");
....
}
}
import org.junit.*;
导入静态org.junit.Assert.*;
公共类JavaSourceViewerTest{
@试验
public void testJavaSourceViewer(){
字符串url=“…”;
JavaSourceViewer jsv=新的JavaSourceViewer();
//在这里调用方法来解析站点
jsv.XXX方法(…)
....
//在这里给您打电话,如:
//-例如。
assertEquals(jsv.getOutputFile(),“D://webcontent.txt”);
....
}
}
要执行Junit,请使用IDE(如eclipse)或将Junit.jar文件放在类路径中并从控制台执行:
java org.junit.runner.JUnitCore JavaSourceViewerTest您的步骤不正确。这样做肯定会有帮助,3,然后1,然后2。这样做会迫使你从功能和单位的角度来思考。结果代码是可测试的,不需要做任何特殊的事情。Test first除了为您提供安全网之外,还可以指导系统的设计 p.S.不要在测试前尝试编写代码。正如你所看到的,它既不是自然的,也不是很有价值的 现在,为了测试第一个单元,您可以将
google.com
中的string
和html
与一些现有的字符串进行比较。但如果谷歌改变它的页面,这个测试用例将被破坏。另一种方法是,只需从头部检查HTTP代码,如果是200,就可以了。只是个主意
对于第二个,您可以通过读取文件,将从网页读取的字符串
,与写入文件的字符串进行比较。您需要提取这样的方法
package abc.def;
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 {
FileOutputStream fout = new FileOutputStream("D://web-content.txt");
writeURL2Stream(url, fout);
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());
}
}
private static void writeURL2Stream(String url, OutputStream fout)
throws MalformedURLException, IOException {
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;
while ((c = r.read()) != -1) {
System.out.print((char) c);
fout.write(c);
}
}
}
好了,现在您可以编写JUnit测试了
package abc.def;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import org.junit.Test;
import junit.framework.TestCase;
public class MainTestCase extends TestCase {
@Test
public static void test() throws MalformedURLException, IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JavaSourceViewer.writeURL2Stream("http://www.google.de", baos);
assertTrue(baos.toString().contains("google"));
}
}
GoogleJUnit教程
,看看你能学到什么。你首先需要把你的方法分解成可测试的、更小的方法。到目前为止,我已经在Google上搜索了JUnit教程,学到了一些关于如何为加法/减法等基本程序编写JUnit测试用例的知识。但是,我仍然找不到多少帮助来为我的这个程序编写junit测试用例@mauhiz:你能告诉我我应该打破并测试哪种方法以及如何测试吗?@Ryan Malhotra,你的思路是正确的,总是尝试在编程中遵循功能块,这将帮助很多很多场景,所以现在如果你这样做,那么做将非常简单,做junits测试用例。我仍然找不到多少帮助来编写junit测试用例
。扪心自问:在这个函数之后,我应该期待什么?我不应该期待什么?我可以在不同的场景中对每个功能的所有可能输入进行分组吗?但您能详细说明这一行的实际功能吗assertTrue(baos.toString().contains(“google”);'我认为这只是检查提取的html是否有一个字符串“google”。在这种情况下,我还能测试什么呢?单元测试测试代码的基本功能。代码没有做更多!因此,如果不生成重复的、过时的或不及时的代码,就无法编写更多的测试。
String str = "";
URL oracle = new URL("http://www.google.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
File file = new File("C:/Users/rohit/Desktop/rr1.txt");
String inputLine;
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
bw.write(inputLine);
}
bw.close();
in.close();