0

I am new to jUnit. Can not figure out how to test handled exception.

 public File inputProcessor(String filePath){
    File file = null;
    try {
        file = new File(filePath);
        Scanner input = new Scanner(file);
    } catch (FileNotFoundException e) {
        System.out.print("Check your input file path");
        e.printStackTrace();
    }
    return file;
}

Now, want to test with a invalid file path to check whether exception is thrown and caught properly. I wrote this

@Test (expected = java.io.FileNotFoundException.class)
public void Input_CheckOnInvalidPath_ExceptionThrown() {
    Driver driver = new Driver();
    String filePath = "wrong path";
    File file = driver.inputProcessor(filePath);
      }

But as I have already caught my exception its not working. Test is failing . Any help will be great!! thnx

5
  • 1
    If the exception isn't thrown even though you provoked it, then it was caught correctly. What's the problem? Commented Sep 22, 2013 at 14:33
  • 2
    Why are you testing for a behavior which the method doesn't exhibit in the first place!? Commented Sep 22, 2013 at 14:33
  • Have you checked the solution proposed here and here Commented Sep 22, 2013 at 14:34
  • As stated in @TreyJonn's link: Tests are supposed to test exposed behavior, not implementation details. If you need to test only parts of a method (i.e. how the exception is handled specifically), it is a sign that your method already does too much and needs to be split into several methods that can then be tested (but which maybe should not be public). Commented Sep 22, 2013 at 14:37
  • @rocketboy didnt get your question. Commented Sep 22, 2013 at 14:55

2 Answers 2

3

You need to test your method's behavior, not its in implementation details.

If the correct behavior of your method is to return null when the file does not exist, you just need

@Test
public void Input_CheckOnInvalidPath_ExceptionThrown() {
    Driver driver = new Driver();
    String filePath = "wrong path";
    assertNull(driver.inputProcessor(filePath));
  }

If the correct behavior of your method is to print a particular message to System.out when the file does not exist, and you want to test that, then you can create a mock PrintStream, use System.setOut(PrintStream) to set it, call your method, and then test that the PrintStream was invoked correctly. Mockito can help you do that -- maybe. I think you run the risk of testing the implementation detail that System.out.println() vs many System.out.print() is invoked. (You probably shouldn't be testing how e.printStackTrace() is implemented.)

If the correct behavior is both, then you need both checks.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Jeremy for a clear and concise answer. "You need to test your method's behaviour, not its in implementation details " - I never think of this concept.
I literally said the same in my comment above: "Tests are supposed to test exposed behavior, not implementation details."
Yes sure you did! Thank you. As I said just started to grasp the whole idea of unit testing, Its easy to understand with snippets!
1

The exposed behavior you need from the method is the appearance of appropriate lines in System.out and System.err. In your test code, you can replace System.out and System.err with your own PrintStream. See System.setOut and System.setErr.

Base each PrintStream on e.g. a StringWriter. That way, in your test code you can pick up the String representing what, if anything, the method wrote to each output stream, and do tests on it.

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.