Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Testing Socket code

 
James Birchfield
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a socket server application and wish to test the socket handling classes of this application. When a connection is made, a client handler is created by passing it a reference to the socket. I want to use something like MockSocket to pass to the constructor of the class, but since MockSocket does not implement java.net.Socket (ratehr alt.java.net.Socket), it does not work.
Does any one have an example of how to use MockSocket, or be willing to share strategies for acheiving the testing of a socket?
Thanks!
Birch
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, we seem to be speaking about http://www.mockobjects.com/ ?
Your client should accept an alt.java.net.Socket. If you want to give it a real Socket, you should use alt.java.net.SocketImpl (which is used a wrapper for a java.net.Socket).
Did that help?
 
James Birchfield
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
OK, we seem to be speaking about http://www.mockobjects.com/ ?
Your client should accept an alt.java.net.Socket. If you want to give it a real Socket, you should use alt.java.net.SocketImpl (which is used a wrapper for a java.net.Socket).
Did that help?

Well, I would rather not change existing code to accept a different Socket class for the sake of testing. I might try to give it a test though on some new code to see if I can get it to work the way I want to.
But does anyone have any strategies for testing socket based code? Having my 'real' code depend on testing code just feels bad.
Birch
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by James Birchfield:
But does anyone have any strategies for testing socket based code? Having my 'real' code depend on testing code just feels bad.

alt.java.net.Socket *is not* testing code - it is an abstraction from the actual Socket and part of your production code. It is needed because Sun doesn't provide an Interface for sockets. In a statically typed language like Java, you will have to live with such workarounds if you want to unit test your programs. (In fact they also make your code better decoupled and therefore more flexible. That's a good thing!)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic