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

Testing a HTTP client

 
Miriam van Soest
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about mocking/stubbing the socket or something like that?
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Miriam van Soest
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic