hi, I am learning Java with "learning by doing" and I am creating a small network bot to learn things. My question related to this that how can I create test cases to test network resource usage (socket) without having a real network? What kind of (if any) stub can I apply to make my application believed that there is a right or wrong behaving network (depending on the actual testcase). Hope U get my idea, Thanx in advance for any comment! Gabor
You don't have to have a network to use Sockets so long as TCP/IP is set up on your machine. Since you are posting to this forum thru the internet, that is a safe assumption. You can create a server which uses a ServerSocket on some predetermined port in one JVM and then create a client that connects to that server using a Socket in a different JVM. You can even connect to the server using telnet to test it before writing any client code.
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Joined: May 23, 2003
hi, Thanks for reply Morris. Sounds quite cool trick. The thing is I make development on an other machine than I am posting to this site from. How can I try my server with telnel? And what is the difference between having TCP/IP and using sockets? There is a need for some underlying trasport protocol to transfer bits from my computer to the Ranch server... Where is the socket appears in the model? "E vita semper considera pars candidior." What is the meaning of this? Gabor
Joined: Jan 30, 2002
Gee Gabor, you are an inqusitive one. How can I try my server with telnel? Once you have a server running that is listening on a specific port, you just fire up telnet and connect to localhost on that port. Assuming that your server starts the conversation, you should immeadiately see some text in your telnet window from your server. And what is the difference between having TCP/IP and using sockets? TCP/IP is a network transport. A Socket is a connection point for communicating over some network transport or protocol. Since Java does not support raw sockets TCP/IP is the only game in town. Where is the socket appears in the model? Not exactly sure what you're asking. "E vita semper considera pars candidior." What is the meaning of this? It's Latin for Always look on the bright side of life.
To take a completely different tack on an answer ... One currently hip testing technique uses "mock" objects. Say you had an object responsible for low-level network communication and an application logic object that uses the low-level one. And the second object with the real application logic is the one you want to test. You "trick" the application object into test mode by giving it a mock communication object instead of the real communication object. When it says "send these bytes" the mock object indicates succss but doesn't really send them. If the application expects some response, the mock object would have to be coded to send the expected reply. The mock object might keep track of the number of messages, number of bytes or actual message content. The success or failure of the test might be whether or not a particular message was sent. The test code looks like:
Note that a MockCommObject is assigned to a commObject variable. Presumably MockCommObject and the real comm object both implement the CommObject interface. There are other ways to trick the object under test into using a mock, such as a CommObjectFactory that knows about test mode. You asked about testing without a network. This kind of mock object is good for removing dependencies on other classes, networks, servers, databases, etc. so you can unit test one class, not a whole system of classes. BTW: If you're not into JUnit, give it a look at JUnit.org. It's a great test framework to help you write and run unit tests. [ May 30, 2003: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: May 23, 2003
thanks Michael and Stan! I had to ask to learn and pull out :-)) explanation and experience from old geeks:-)) I think I will try Stan's solution instead. Since I already started to use JUnit (though I am quite basic level) I go for such kind of unit test. So, did I understood well that I had to create some "dummy" class (and then object) which implements the same interface as my "real" class/object that handles the real network and soforth resources? And actually this dummy object pretends as being a right or wrong behaving "resource" (depending on the actual testcase). Am I right? Gabor And sg for Michael: "Az elet nem habostorta!" )