How to mock FileInputStream and other *Streams

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

    You can mock FileInputStream by using PowerMockRunner and PowerMockito. See the below code for mocking-

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({
            FileInputStream.class
    })
    public class A{
    
     @Test
            public void testFileInputStream ()
                        throws Exception
           {
               final FileInputStream fileInputStreamMock = PowerMockito.mock(FileInputStream.class);
               PowerMockito.whenNew(FileInputStream.class).withArguments(Matchers.anyString())
                                   .thenReturn(fileInputStreamMock);
        //Call the actual method containing the new constructor of FileInputStream 
    
            }
    }
    

提交回复
热议问题