简体   繁体   English

如何在 Java 中生成 JUnit 测试用例?

[英]How to generate JUnit Test case in Java?

I am practising JUnit test cases and currently working on a problem which is as follows:我正在练习 JUnit 测试用例,目前正在解决如下问题:

  • To read HTML from any website say " http://www.google.com " ( Candidate can use any API of inbuilt APIs in Java like URLConnection ).要从任何网站读取 HTML,请说“ http://www.google.com ”(候选人可以使用 Java 中内置 API 的任何 API,例如 URLConnection)。
  • Print on console the HTML from the URL above and save it to a file ( web-content.txt) in local machine.在控制台上从上面的 URL 打印 HTML 并将其保存到本地机器中的文件 (web-content.txt)。
  • Write JUnit test cases for the above program.为上述程序编写 JUnit 测试用例。

I've successfully achieved first steps but when I am running JUnit Test Case its showing Failure.我已经成功地完成了第一步,但是当我运行 JUnit 测试用例时,它显示失败。

ReadFile.java读取文件

package com.test;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;

public class ReadFile 
{
    static void display(String input,OutputStream fos)
    {
        try
        {
            URL url = new URL(input);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
            Reader reader = new InputStreamReader(stream);
            int data=0;
            while((data=reader.read())!=-1)
            {
                System.out.print((char)data);
                fos.write((char)data);
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
    public static void main(String[] args) 
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String input =null;
        FileOutputStream fos =null;
        System.out.println("Please enter any url");
        try
        {
            input = reader.readLine();
            fos = new FileOutputStream("src/web-context.txt");
            display(input,fos);
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

ReadFileTest.java读取文件测试.java

package com.test;

import static org.junit.Assert.*;

import java.io.ByteArrayOutputStream;

import org.junit.Test;

public class ReadFileTest {

    @Test
    public void test() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ReadFile.display("http://google.co.in", baos);
        assertTrue(baos.toString().contains("http://google.co.in"));
    }

}

I am getting following error while running JUnit Test in Eclipse:在 Eclipse 中运行 JUnit 测试时出现以下错误:

java.lang.AssertionError java.lang.AssertionError at org.junit.Assert.fail(Assert.java:86) at org.junit.Assert.assertTrue(Assert.java:41) at org.junit.Assert.assertTrue(Assert.java:52) at com.test.ReadFileTest.test(ReadFileTest.java:15) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown java.lang.AssertionError java.lang.AssertionError at org.junit.Assert.fail(Assert.java:86) at org.junit.Assert.assertTrue(Assert.java:41) at org.junit.Assert.assertTrue(Assert) .java:52) at com.test.ReadFileTest.test(ReadFileTest.java:15) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown

I want that the JUnit Test Case will return true.我希望 JUnit 测试用例将返回 true。

What's not working here is :在这里不起作用的是:

assertTrue(baos.toString().contains("http://google.co.in"));

and what would work is什么会起作用

assertTrue(baos.toString().contains("google.co.in")); // note the difference

Make something like that:做这样的事情:

static String display(String input) {
    try {
        URL url = new URL(input);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
        Reader reader = new InputStreamReader(stream);
        int data = 0;
        StringBuilder builder = new StringBuilder();
        while ((data = reader.read()) != -1) {
            builder.append((char) data);
        }
        return builder.toString();
    } catch(Exception e) {
        e.printStackTrace();
        return null;
    }
}

I don't know why you use ByteArrayOutputStream我不知道你为什么使用 ByteArrayOutputStream

And now for your test case:现在对于您的测试用例:

@Test
public void test() {
    String data = ReadFile.display("http://google.co.in");
    assertTrue(data != null);
    assertTrue(data.contains("http://google.co.in"));
}

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

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