繁体   English   中英

需要编写JUnit测试用例

[英]Need to write JUnit Test case

我是一个更新鲜的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());
    }
  }
}    

现在我需要第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);
        }
    }
}

好的,现在您可以编写JUnit-Test了。

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

    }
  }
  • 首先将代码从main方法移动到JavaSourceViewer中的其他方法。
  • 第二,让这些方法返回你可以测试的东西。 例如输出文件或Reader的文件名。

然后为单元测试创​​建一个类

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

}

要执行Junit,请使用IDE(如eclipse)或将junit.jar文件放在类路径和控制台中:

java org.junit.runner.JUnitCore JavaSourceViewerTest

你的步骤不对。 以这种方式做这肯定会有所帮助,3,然后是1,然后2.以这种方式这样做会迫使你思考功能和单位。 结果代码是可测试的,没有做任何特殊的事情。 除了为您提供安全网之外,首先测试还会指导您的系统设计。

PS 切勿在测试前尝试编写代码。 正如你所看到的那样, 它并不是很自然,也不是很有价值

现在,要测试第一个单元,您可以将string (来自google.comhtml )与一些现有string 但是,如果谷歌改变它的页面,那个测试案例就会破裂。 其他方式,只是检查标题中的HTTP代码,如果是200,你没事。 只是一个想法。

对于第二个,您可以通过读取文件将您从网页读取的字符串与您写入文件的string进行比较。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM