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?
Asked
Active
Viewed 2,431 times
11

yegor256
- 102,010
- 123
- 446
- 597
-
This question is related: http://stackoverflow.com/questions/2083647/writing-a-java-net-socketimplfactory – yegor256 Dec 19 '11 at 15:47
-
Also related: http://stackoverflow.com/questions/2257901/mock-runtime-getruntime – Rich Dec 20 '11 at 08:02
3 Answers
2
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.

Peter Lawrey
- 525,659
- 79
- 751
- 1,130
0
According to the javadoc, you should be able to use SocketFactory#getDefault() to get the default SocketFactory for your environment.
BTW: You might also want to look at this RFE/bug which declares that SocketImplFactory is basically a dead-end: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4245730

Mark Rotteveel
- 100,966
- 191
- 140
- 197
-
You're right, but I'm talking about `SocketImplFactory`, which generates instances of `SocketImpl` class. Class `java.net.Socket` has nothing to do with all this. – yegor256 Dec 19 '11 at 15:42
0
Looking at the source code of the Socket class, there is no original factory - the constructors check to see if factory
is null, and if it is, they just assign impl
to be a new PlainSocketImpl()
.

Rich
- 15,602
- 15
- 79
- 126
-
As far as I understand there is no such thing as "the source code of the Socket class". Every JDK has its own sources, which may differ from implementation to implementation. – yegor256 Dec 19 '11 at 15:37
-
Perhaps it was naive of me but I assumed you were using the Oracle JDK 6, in which case there are specific sources to look at - I did this and used it to explain that there is no original factory to get(). If you are able to do it, I agree with @Peter Lawrey that using a service is the best method. I had to do the same with Runtime.getRuntime(). http://stackoverflow.com/questions/2257901 – Rich Dec 20 '11 at 08:01
-
It's a third party library inside my application, that is using Sockets. I can't change it anyhow. – yegor256 Dec 20 '11 at 08:03