aspose file tools*
The moose likes Testing and the fly likes Testing Socket code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Testing Socket code" Watch "Testing Socket code" New topic
Author

Testing Socket code

James Birchfield
Greenhorn

Joined: Oct 22, 2002
Posts: 3
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

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
James Birchfield
Greenhorn

Joined: Oct 22, 2002
Posts: 3
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

Joined: Jul 11, 2001
Posts: 14112
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!)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Testing Socket code