开发者

Test method or try-catch?

开发者 https://www.devze.com 2023-03-12 05:54 出处:网络
I was hoping that someone could tell me the best practice for the scenario I am running into with testing my code via junit4.

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() {
          Str开发者_StackOverflow中文版ing 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消