Testing what's written to a Java OutputStream

后端 未结 3 1877
既然无缘
既然无缘 2021-01-07 17:45

I am about to write junit tests for a XML parsing Java class that outputs directly to an OutputStream. For example xmlWriter.writeString(\"foo\"); would produce

3条回答
  •  自闭症患者
    2021-01-07 17:51

    It's simple. As @JonSkeet said:

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // pass the baos to be writed with "value", for this example
    byte[] byteArray = baos.toByteArray();
    Assert.assertEquals("value", new String(byteArray));
    

提交回复
热议问题