Mocking Java InputStream

You could just use a ByteArrayInputStream and fill it with your test data.

@Brad's example from the comments:

InputStream anyInputStream = new ByteArrayInputStream("test data".getBytes());

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

InputStream stubInputStream = 
     IOUtils.toInputStream("some test data for my input stream", "UTF-8");

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"));

Tags:

Java

Mocking