How to mock FileInputStream and other *Streams

前端 未结 5 1964
暖寄归人
暖寄归人 2021-01-14 11:47

I have class that gets GenericFile as input argument reads data and does some additional processing. I need to test it:

public class RealCardParser {

    pu         


        
5条回答
  •  北恋
    北恋 (楼主)
    2021-01-14 12:23

    I know this isn't the answer that you want.

    The idea of unit testing is to make sure your logic is correct. Unit tests catch bugs where incorrect logic has been written. If a method contains no logic (that is, no branching, looping or exception handling), then it is uneconomical to unit test it. By that, I mean that a unit test costs money - time to write it, and time to maintain it. Most unit tests pay us back for that investment, either by finding bugs, or re-assuring us that there are no bugs in the domain of what is being tested.

    But a unit test for your getBufferedReader method would not pay you back for our investment. It has a finite cost, but zero benefit, because there is no actual logic that can go wrong. Therefore, you should NOT write such a unit test. If your Cobertura settings or your organisational standards require the existence of such a unit test, then those settings or standards are WRONG and should be changed. Otherwise, your employer's money is being spent on something that has an infinite cost:benefit ratio.

    I strongly recommend that your standards are changed so that you only write unit test for methods that contain branching, looping or exception handling.

提交回复
热议问题