Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate java socket connection break?

Tags:

java

I have written java application that makes socket connections with a legacy system.

How can I simulate the connection failure (Eg Socket connection timeout, read timeout, network failure), so that i can add necessary code based on the exception trace ?

like image 697
Hari Avatar asked Sep 26 '26 12:09

Hari


1 Answers

Use a mock framework such as Mockito or EasyMock, mock a Socket object.

Look at the Javadoc for Socket and make your mocked object fail at some point with an Exception thrown by it in your unit tests.

like image 79
fge Avatar answered Sep 28 '26 02:09

fge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!