Unit testing pages with Tapestry and Gaedo

Today we added our first page unit test using tapestry. Tapestry offer some nice mechanisms to easily test the rendered pages (see the tutorial); however some steps can be a bit tricky (essentially if you want to use the gaedo persistence layer in your tests) so I'll give a few hints here.

Tapestry provides a PageTester object that takes care about injection registry loading and page rendering.
The PageTester takes an app package name and app name as parameters in its constructor. Make sure you don't make a mistake in them, because you won't get any error even if the package or app module don't exist, so check it twice if your test is failing without evident reason.
To use the gaedo module, load it by adding it to the PageTester constructor. Moreover some configuration is required to work with a local database environment. All those steps can be done in an extension of the PageTester:
You can now use the render method of the GaedoPageTester to get the Document corresponding to a page and check it's content!
Last point: in the tapestry documentation you'll see that you must use the invoke method of the PageTester with a ComponentInvocation instance to provide a context to a given page. But if you use this, you'll get a compilation error, since the invoke method doesn't seem to exist yet/anymore (?). As explained in this thread, you can use the url passed to the render method to set the context. In the second test you can see we pass "John" as context to the "People" page by requesting the "People/John". Easy, isn't it? Happy testing with gaedo and tapestry!

Gaedo on Ohloh

The open source project Gaedo that we use as persistence layer for our project is now on Ohloh.com!

Gaedo is a persistence library providing dynamic code generation for your queries with strong typing. It's quick and easy to use. And dynamic code generation is awesome. Let's assume you need to retrieve your Users using their login field, you just need to define the following interface:
and gaedo will do the rest for you, using the method and fields types and names! It couldn't be easier to use!

Running Tapestry5 + Maven on Google App Engine

 

1) Checking out Tapestry


As explained on the tapestry website , you should use the maven-tapestry-archetype

 

While I'm writting this article, the lastest stable version of tapestry is 5.1.0.5, but there are an issue concerning tapestry/gae compatibility.

However, this bug is corrected in tapestry 5.1.0.6. If you still want to run tapestry 5.1.0.5 on GAE, you should use this quick fix :

Create a new class org.apache.tapestry5.internal.antlr.PropertyExpressionLexer and put this code on :


 

2) Configuring the project for GAE


Drop a file named "appengine-web.xml" in src/main/webapp/WEB-INF

The file should looks like this :

If you want to know how to synchronize GAE and maven version you should read this excellent article writed by Antoine : http://blog.dooapp.com/synchronize-maven-and-gae-application-version

Of course, you'll need to download the GAE SDK for java from here : http://code.google.com/intl/fr/appengine/downloads.html

3) Running the tapestry project on gae


Before starting to code, I suggest you to try running the simple tapestry example on gae.

 

  • Go to your project
  • mvn package
  • go to GAEHOME/bin
  • ./appcfg.sh update YOURPROJECTHOME/target/YOURPROJECTNAME
  • Enter your mail and password
  • go to http://YOURPROJECTNAME.appspot.com

For now, there are some issues when you run the gae server on your localhost. So I suggest you to test everything you do online.

4) What's Next


Now you can run tapestry on gae, if you want to upload your application with maven, I suggest you to have a look at this plugin : http://code.google.com/p/maven-gae-plugin/

And of course configuring a continuous integration server can be very useful to automatically build and deploy your application: http://hudson-ci.org/