java - 需要编写 JUnit 测试用例

标签 java junit

我是一名新手 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/

相关文章:

java - 从数据库中检索一个值并将其设置为Spring boot中的@Table Name值

java - Intellij 使用混合项目 (maven+gradle)

java - 使用 WireMock 设置 Content-Length HTTP header 作为响应

java - ant 的 junit 任务中的另一个 java.lang.ClassNotFoundException

java - 如何在主包之外构建 JUnit 测试目录?

java - 如何避免重复的 JUnit 测试

java - JAVA中像素改变后的图像实现

java - jexcel API 中的 IndexOutOfBoundsException

java - 修改 ArrayList 中的重复项

java - 单元测试 Java Spark 微框架