Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock an outgoing Socket connection?

In integration tests (JDK 6) I'm trying to catch all outgoing TCP connections and mock them. Looks like I should use java.net.Socket#setSocketImplFactory() method. Works fine at the moment, but I can't understand how I can get an access to original factory, in order to instantiate original JDK-provided SocketImpl class. I need this mostly because I want to let some connections to go out freely, without mocking. Can you suggest some manuals/guidelines/instructions about this problem?

like image 946
yegor256 Avatar asked Dec 19 '11 15:12

yegor256


1 Answers

Instead of mocking a Socket I would create a Socket service for the Socket to talk to. This can capture all the data written and reply in any manner you wish. It can be run in the same test and possibly in the same thread.

like image 154
Peter Lawrey Avatar answered Oct 17 '22 19:10

Peter Lawrey