Mocking Java InputStream

前端 未结 10 1637
既然无缘
既然无缘 2020-11-29 04:40

Please provide pointers to help me mock that java InputStream object. This is the line of code that I would wish to Mock:

InputStreamReader inputData = new I         


        
相关标签:
10条回答
  • 2020-11-29 04:45

    You could use commons-io to create some stub input streams:

    InputStream stubInputStream = 
         IOUtils.toInputStream("some test data for my input stream", "UTF-8");
    
    0 讨论(0)
  • 2020-11-29 04:51

    I disagree with the selected answer for this question. Mocking frameworks like Mockito are nice and all, however when standard java api is available you might consider using that instead.

    i.e.

    BufferedReader reader = new BufferedReader(new StringReader("some string"));
    

    Why use a Mock object in your test classes when you could use a real one with all its state and behaviour?

    To see more about how this works, you could look up the 'decorator' design pattern.

    0 讨论(0)
  • 2020-11-29 04:52
    BufferedReader bufferedReader = org.mockito.Mockito.mock(BufferedReader.class);
    when(bufferedReader.readLine())
      .thenReturn("first line")
      .thenReturn("second line");
    
    org.junit.Assert.when(new Client(bufferedReader).parseLine())
      .thenEquals(IsEqual.equalTo("first line"));
    
    0 讨论(0)
  • 2020-11-29 04:55
    @Test
        public void testReadFile() {
        TestClass ClassName = Mockito.mock(TestClass.class);
         InputStream in = Mockito.mock(InputStream.class);
         InputStreamReader inr =Mockito.mock(InputStreamReader.class);
         BufferedReader bufferedReader =Mockito.mock(BufferedReader.class);
           try {
             PowerMockito.whenNew(InputStreamReader.class).withArguments(in).thenReturn(inr);
             PowerMockito.whenNew(BufferedReader.class).withArguments(inr).thenReturn(bufferedReader);
             String line1 = "example line";
             PowerMockito.when(bufferedReader.readLine()).thenReturn(line1).thenReturn(null);
             method return type = Whitebox.invokeMethod(ClassName, "MethodName", arguement);
             assertEquals("result is::","expected", actual);
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
    
    0 讨论(0)
  • 2020-11-29 04:55
    String testString = "test\nstring";
    InputStream stream = new ByteArrayInputStream(testString.getBytes(StandardCharsets.UTF_8));
    
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
    
    Assert.assertEquals("test", reader.readLine());
    Assert.assertEquals("string", reader.readLine());
    
    0 讨论(0)
  • 2020-11-29 05:00

    Change your object so it is easier to test, something like this:

    public MyObject {
        private InputStream inputStream;
    
        public void setInputStream(InputStream inputStream) {this.inputStream = inputStream;}
    
        public void whatever() {
            InputStreamReader inputData = new InputStreamReader(inputStream);
            bufferdReader = new BufferedReader(inputData);
            bufferdReader.readLine(); 
        }
    }
    

    then when you use your object initialize its inputStream first:

    MyObject myObject = new MyObject();
    myObject.setInputStream(System.in);
    

    Now you have an object where you can test it using any implementation of InputStream you want (ByteArrayInputStream is a good one to try).

    0 讨论(0)
提交回复
热议问题