需要编写JUnit测试用例

Rya*_*tra 6 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());
    }
  }
}    
Run Code Online (Sandbox Code Playgroud)

现在我需要第3步的帮助.

Pet*_*der 3

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

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

好的,现在您可以编写 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"));

    }
  }
Run Code Online (Sandbox Code Playgroud)