2

I am practising JUnit test cases and currently working on a problem which is as follows:

  • To read HTML from any website say "http://www.google.com" ( Candidate can use any API of inbuilt APIs in Java like URLConnection ).
  • Print on console the HTML from the URL above and save it to a file ( web-content.txt) in local machine.
  • Write JUnit test cases for the above program.

I've successfully achieved first steps but when I am running JUnit Test Case its showing Failure.

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

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:

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.

10
  • What exception you have? Commented Dec 14, 2016 at 17:07
  • Please share the entire error log and what is the expected output. Commented Dec 14, 2016 at 17:08
  • 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.sudhir.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 Commented Dec 14, 2016 at 17:08
  • 3
    The assertion error means that your assertTrue came out false. It doesn't give you much info. You need to find out what baos.toString() actually returned. Asserts take a "String" parameter that is printed when they fail, so you probably want the assertTrue a string like "baos was not as expected, instead it was:"+baos.toString() (That string should be the first parameter of the assert, your existing parameter should be the second). Commented Dec 14, 2016 at 17:11
  • 1
    Sudhir, please could I trouble you to add in your stack trace or exception into this question, as if you had edited your question on the same day you asked this question. That is better preserved in the question, since it seems to be related to the problem you experienced, and may well be useful for a future reader. They are not likely to find it in the comments. Once you have made this edit, please ping me with @halfer and I will undownvote. Thank you! Commented Dec 28, 2016 at 19:10

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.