gpt4 book ai didi

java - 需要编写 JUnit 测试用例

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:14:23 25 4
gpt4 key购买 nike

我是一名新手 Java 开发人员。但我对编写 Junit 测试用例了解不多。我很快就要参加工作考试了。他们要我为此编写一个程序

  1. 要从任何网站阅读 HTML,请说“http://www.google.com”(您可以使用 Java 中内置 API 的任何 API,例如 URLConnection )
  2. 在控制台上打印来自上面 url 的 HTML 并将其保存到文件 (web-content.txt)在本地机器上。
  3. 上述的 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());
}
}
}

现在我需要第三步的帮助。

最佳答案

你需要像这样提取一个方法

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"));

}
}

关于java - 需要编写 JUnit 测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12526819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com