Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to fake a client

 
Nick Sher
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My web application reads/writes xml document via a url connection. Is there anyway I can fake this connection and read the document from a file? I would like to be able to test all the cases of 5xx, 2xx http errors.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use an interface to define the XML retrieval method. During testing use an implementation that reads from a local file. This is one big reason we code to interfaces not implementations--testability.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11944
212
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might also want to look at Mockito and TestNG.

Mockito will help you when you need to mock out calls to real back-end systems, whether they be databases or web-services, or something else.

TestNG is similar to JUnit, but has the added benefit (for you) that it understands the concept of data driven testing.
 
Nick Sher
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David and Andrew.

I started using Jetty to serve xml files at the desired endpoint. I read in 'JUnit in Action' that it can also be used to test the HTTP error cases, for example, service not available, etc.

I hope this will help in the long run.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Sher wrote:I started using Jetty to serve xml files at the desired endpoint.

Yuck. Another thing that can go wrong during the tests.
 
Nick Sher
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:
Nick Sher wrote:I started using Jetty to serve xml files at the desired endpoint.

Yuck. Another thing that can go wrong during the tests.


How is that possible?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is it possible that contacting a different server (even if on the same machine) and going through a complete HTTP request cycle could go wrong, as opposed to just reading a file off of a disk?! (Besides that it'll take longer.)

The whole point of coding to an interface is so you can plug in implementations. Being able to plug in implementations means you could just *mock* the XML service and implement almost nothing, rather than making sure the server is started.

You've turned a unit test into an integration test, integrating something that doesn't even exist in real life. And for the *integration* test you now have to make your Jetty simulate various failure modes, instead of just mocking the service and supplying the results you need.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic