Showing posts with label Jersey. Show all posts
Showing posts with label Jersey. Show all posts

Tuesday, March 20, 2012

REST-assured!

It's been a little while since my last post; I've been very busy professionally and personally.

For example, over the last month, I've weighed EasyMock and Mockito, then chosen the latter and cranked out a first draft of tests to cover a business logic layer already written.

This last week or so, however, I’ve been playing around with REST-assured in my spare time in an effort to leave no stone unturned on the way to covering every path in our restlet with a test. This is a very lightweight, open-source (Google-hosted) framework that allows you to test your JAX-RS implementation.

ReST testing is brittle

This sort of testing is a little brittle to say the least. It requires...

— Tomcat running the restlet.
— a database (in my case anyway) initialized with at least a modicum of test fodder.

In my case, I targeted one entity in my server that amounts to a collection of 172 guaranteed objects in my database. So, ...

— in order to test the HTTP POST, PUT and DELETE verbs, assumptions had to be made as to the existence of objects (being able to add rows to the database, some to update and some to delete for completeness).

The result is or can be somewhat destructive.

That's three strikes and the first two clearly mean this is not unit testing anyway, but something else (although, you code in JUnit).

I'm in the REST-assured community trying to find out how to solve the third problem. Still, it's a back-burner project as it's not super important and there aren't a tremendous number of code paths involved. These paths may turn out to be the 20% I don't cover.

But wait: There are advantages!

Still, it's pretty cool: the code reads wells, is simple and very self-documenting.

Second, it is a great formalism for stating exactly what URIs are implemented (handled by my ReSTful service) and how they should behave.

Last, it covers creating failure situations and explicit checking for particular HTTP status code returns.

Ooooo...

These last two points scratch (nay, gash deeply with long nails!) my TDD fascination and are the real reason I even walked this path.

Check out my notes and sample code here.

Thursday, July 8, 2010

RESTful web services in Java


Today I finished annotating a tutorial on this topic. The link at the very end of this post—past the social commentary.

Many, including me, publish tutorials; this is the salvation of the computer software industry in a day where no university program could keep up with the frenetic pace of technology even if it were possible for industry workers to maintain continuous enrollment in such an institution.

In the world of JEE alone, there must be more ways to implement Model/View/Controller separation than Paul Simon knows for dumping an old lover.

This said, many tutorials are written with very questionable literary skill. More still are written by experts who've long gotten beyond the elemental skills that others wanting to capitalize on their knowledge are still in pursuit of. The author can scarcely burden himself with technical accuracy, still less with organizational completeness in his expression's vehicle: the tutorial itself. He has no time at all to coddle the beginner through the process of following it. More's the pity.

In one recent tutorial I reviewed and annotated, the author introduced some new HTML code neither revealing the filename it should garnish nor even which, of half a dozen Eclipse projects written over the course of the tutorial, it should find its way into.

Well, I'm one of the great numbers of dummies.

So, I drop bread crumbs à la Hansel und Gretl in tutorials that interest me (not to mention in my own). I try to cross-reference more difficult operations, especially sub-operations, so that someone struggling to complete his "homework" isn't stopped dead in his tracks, unable to learn the subject because he doesn't know the Eclipse IDE well enough, how to build a library as a JAR, refresh a container server like Tomcat, work around an HTTP status 404 error, etc.

How does this benefit me? Am I fishing for compliments? Maybe. More likely am I proving to myself that I grok the tutorial I'm reviewing or the subject on which I'm writing my own.

My tutorial notes on RESTful Web Services in Java and Jersey can be found here.