Thursday, October 2, 2008

JSFUnit the Easy Way!!

JSFUnit tests are easy to write and understand. This comes across clearly whenever I talk about it at a conference and I see faces light up. They "get it". However, it's also clear that the hardest part of JSFUnit is getting started. Once you get over that hump, developers really like the tool. But until now, setting up JSFUnit for the first time required quite a bit of work.

Enter the new JSFUnit deployer for JBoss AS 5. Now, here is all you need to do to set up JSFUnit for testing:
  1. Copy jboss-jsfunit-microdeployer.jar to /server/default/deployers
  2. Rename your war with a "-jsfunit" suffix (such as "mywar-jsfunit.war")
  3. Add your test classes to the WAR or put them in /server/default/jsfunitTests
  4. Restart JBoss AS 5 and run your tests.
That's it. In fact, with a simple edit to the deployer's descriptor you can even get rid of step 2.

This is all made possible because of the redesigned microcontainer of JBoss AS 5. You can take an ordinary WAR and add servlets, filters, and classes on the fly as it is being deployed. I've been wanting this kind of functionality for as long as I can remember. It's perfect for adding test tools, fixtures, or monitoring. Before now you had to bundle this stuff with the application - stuff that really doesn't belong in your WAR.

The interesting part is that now instead of thinking of a WAR or EAR as "my application", we can truly think of it as a reuseable deployment unit. It's just another component that can be decorated or combined with other components. While this view provokes a lot of questions (security, name collisions, etc.), the mind reels with the possibilities that this represents.

So long, and thanks for all the fish,



Ales Justin said...

Where's the source code? :-)

Stan Silvert said...

The source is in the JSFUnit repo.

Ales Justin said...

Few comments to this deployer:

It shouldn't extend an optional deployer.
Rather extend AbstractSimpleVFSRealDeployer.
So that it only applies to those units that have JBossWebMD attachment present == wars.

!vFile.isLeaf() doesn't mean it's a directory. ;-)
It will also apply for jar/zip, hence always recursively checking whole jar.

No need to build schema all the time. Add / remove on deployer's create / destroy.

Stan Silvert said...

Thanks Ales. Still getting my feet wet here.

For those of you watching this discussion, Ales is the one who wrote a lot of this deployer stuff.

I fixed #1.

For #2, I think only the comment is wrong. While it's not technically a directory, it is a directory in the virtual (VFS) sense, right? Anyhow, I believe this is what I want because I want to add any file/directory specified, plus any descendant.

For #3, I don't understand at all. Obviously, I'm missing something about how all this works.

Ales Justin said...

For #3, see how we do it in the deployer from the link - adding metadata class to xb binding in create, removing it at destroy.

Anna said...

Great and Useful Article.

JSF Online Training

JSF Training

JSF Interview Questions

JSF Tutorials