SequenceObjectInputStream implements ObjectInputStream<T> { @Override public T readNext() { if (in == null) { return null; } T c = in.readNext(); if (c == null) { try { nextStream(); } catch (Exception e) { throw new IllegalStateException("Next stream failed", e); } return readNext(); } return c; } SequenceObjectInputStream(Enumeration<ObjectInputStream<T>> enumeration); @Override T readNext(); @Override void close();  }
@Test public void testReadNext() throws Exception { when(objectInputStreamMock1.readNext()).thenReturn(RETURN_VALUE1); assertThat(subject.readNext(), is(RETURN_VALUE1)); verify(objectInputStreamMock1).readNext(); when(objectInputStreamMock1.readNext()).thenReturn(null); when(objectInputStreamMock2.readNext()).thenReturn(RETURN_VALUE2); assertThat(subject.readNext(), is(RETURN_VALUE2)); verify(objectInputStreamMock2).readNext(); verify(objectInputStreamMock1).close(); when(objectInputStreamMock2.readNext()).thenReturn(null); assertThat(subject.readNext(), is(nullValue())); verify(objectInputStreamMock2).close(); }