Search code examples
javaunit-testingsocketstestingserversocket

How to reproduce java.net.SocketException: Broken pipe


I need to right test that ensures that i fixed java.net.SocketException: Broken pipe. How to reproduce it if I have socket in java code?


Solution

  • Assuming that you want to reproduce it on a unit test, you should use Mockito framework to reproduce this exception.

    doThrow(new SocketException()).when(mockedObject.methodReturningVoid(...))
    

    You can find more information here