Hari
Hari

Reputation: 397

How to simulate java socket connection break?

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 ?

Upvotes: 3

Views: 1976

Answers (2)

fge
fge

Reputation: 121712

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.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533492

Do you want to trigger an actual failure or test your code works in different situations?

If its the latter, I would create a simple mock server, or if this is not practical, a proxy server. Close the outbound or incoming connection, or stop sending data at a certain point. At this point you can test your code behaves as expected.

Upvotes: 1

Related Questions