jQuery in Action, 2nd edition*
The moose likes Testing and the fly likes How to fake a client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "How to fake a client" Watch "How to fake a client" New topic
Author

How to fake a client

Nick Sher
Ranch Hand

Joined: Nov 10, 2008
Posts: 78
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.


Nick
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Mar 28, 2003
Posts: 11278
    
  59

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.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Nick Sher
Ranch Hand

Joined: Nov 10, 2008
Posts: 78
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Nov 10, 2008
Posts: 78
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

Joined: Sep 29, 2008
Posts: 12617

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to fake a client
 
Similar Threads
How to make a effective JProgressBar
How to make sure that file has been read.
WA #1.....word association
can SAX or DOM parse only an XML string at a time?
Most Inappropriate Google Suggests Of All Time