How to convert ByteArrayOutputStream to Reader

java, unit-testing

Solution

You can try

ByteArrayOutputStream baos = 
Reader reader = new InputStreamReader(new ByteArrayInputStream(baos.toByteArray()));

a simpler solution would be to check the contents of the buffer.

assertEquals(expected, baos.toString().trim());

Problem

Having ``` ByteArrayOutputStream b = ...; //some code under test writes to b Reader result = (convert b to reader); IOUtils.contentEqualsIgnoreEOL(expected, result); ``` How to convert a `ByteArrayOutputStream` to a `Reader`?

Original source