0

I was hoping that someone could tell me the best practice for the scenario I am running into with testing my code via junit4.

My pseudocode is as follows:

public class TestClass { private String readFromFile(String filePath) { use BufferedReader, get content file input append BufferedReader to StringBuffer return string } @Test public void testMethodOne() { String s = readFromFile(C:\fileOne); doStuff(s); } } 

The issue that I am having is that BufferedReader can throw an IOException. If the readFromFile() method is not a method in the test I am classing (I only need it for these test scenarios), do I annotate it with @Test (expected = IOException.class) anyway, or should I use a try-catch block?

Thank you very much!

2
  • 2
    StringBuffer can throw an IOException? Commented Jun 10, 2011 at 15:58
  • I'm sorry, I'm using it in conjunction with BufferedReader- I'll update my post to reflect that. BufferedReader is throwing the IOException Commented Jun 10, 2011 at 16:00

1 Answer 1

2

If it throws an exception and you're not expecting it to, then it's an exceptional situation. If the file cannot be read and the test cannot be performed, it's an error, it's not something related to the tests themselves.

Add throws to the readFromFile method and to the test methods using it.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.