Mocking an ObjectInputStream

Viewed 1621

When i try to mock ObjectInputStream object i get a NullPointerException. More precisely, when this line is called: when(inputStream.readObject()).thenReturn(new Person("Doe", "John", "123"));

Code fragment:

@RunWith(MockitoJUnitRunner.class)
public class ConnectionTest{
  ...
  @Mock
  private ObjectInputStream inputStream;
  ...
  @Test
  public void test(){
   ...
    when(inputStream.readObject()).thenReturn(new Person("Doe", "John", "123"));
  ...
  }
}

Normally, when you initialize ObjectInputStream you have to pass an InputStream object as a constructor argument and i bet this is the problem here - there's no InputStream assigned to the ObjectInputStream.

How should i mock the ObjectInputStream properly then?

3 Answers
Related