wood burning stoves 2.0*
The moose likes Testing and the fly likes Testing a HTTP client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Testing a HTTP client" Watch "Testing a HTTP client" New topic
Author

Testing a HTTP client

Miriam van Soest
Greenhorn

Joined: Dec 21, 2005
Posts: 4
Hello,

I wrote a client that sends a message via HTTP and gets a certain response. Now I want to unit test the HTTP sending. I did stub the client so I can test my other classes, but I would love to have the HTTP sending also in my automatic unit tests. Is there some "dummy HTTP server" I can start from code, and configure to give a certain response back? I probably am not the first one to need something like this, so I rather search first, before I start programming
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
What about mocking/stubbing the socket or something like that?


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
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Or, if you don't feel like testing by mocking sockets (I assume you didn't really implement the HTTP protocol yourself anyway, did you?), you can use an embeddable web server such as Jetty.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Miriam van Soest
Greenhorn

Joined: Dec 21, 2005
Posts: 4
Well we could mock sockets of course. But I am one of the lazy programmers. If somebody else did it already succesfully, I don't want to build it again

Jetty is harder, because we need to run it on a build machine too.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Miriam van Soest:
Jetty is harder, because we need to run it on a build machine too.

You can run Jetty in-memory, i.e. start it up using plain old Java code. See here for an example.
 
Don't get me started about those stupid light bulbs.
 
subject: Testing a HTTP client
 
Similar Threads
Web Service response not getting.
Cant understand the example
java.lang.IllegalStateException
sendRedirect() method
putting request inside response