How to mock FileInputStream and other *Streams

前端 未结 5 2020
暖寄归人
暖寄归人 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:11

    I would first extract the creation of the Stream into a dependency. So your RealCardParser gets a StreamSource as a dependency.

    Now you can take appart your problem:

    1. for your current test provide a mock (or in this case I would prefer a fake) implementation returning a Stream constructed from a String.

    2. Test the actual StreamSource with a real file, ensuring that it returns the correct content and what not.

提交回复
热议问题