Hai friends, please tell me how to write Junit for the below method,

public static FileInputStream  readAsFileInputStream(String fileName,String fileLocation) throws IOException
    {
        FileInputStream fileinputstream = null;
        File file = null;
        try
        {
            file = new File(fileLocation,fileName);
            fileinputstream = new FileInputStream(file);
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            fileinputstream.close();
        }
        return fileinputstream;
    }

Please help me in this problem,
Thanks.

Recommended Answers

All 3 Replies

First use code Tags.

public static FileInputStream readAsFileInputStream(String fileName,String fileLocation) throws IOException
{
FileInputStream fileinputstream = null;
File file = null;
try
{
file = new File(fileLocation,fileName);
fileinputstream = new FileInputStream(file);
}
catch(IOException ex)
{
ex.printStackTrace();
}
finally
{
fileinputstream.close();
}
return fileinputstream;
}

It is a static method. You just need to implement a test method for JUnit using the method. What you want is to check whether it throws exception properly if something goes wrong. Also, you need to check if it can return what you want. That's all. Do you know how to use JUnit by the way?

Tahnks lot Taywin, Now i get the idea for using JUnit for this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.