Unit Testing Web User Interfaces
Why haven't developers of web user interface frameworks got the unit testing bug yet? I was just given a demo of a work-in-progress written using JSF. The person giving the demo selected an item from a pull-down menu and we were treated to a nice little exception explaining that #{someObject.someProperty} did not, in fact, reference an actual property on an actual object. Well, I'm sorry, but that is precisely the kind of issue that should be caught by a unit test. A good unit testing framework for JSF would also check that actions perform as expected, that validators pass values they should pass and reject the ones they should reject, that error conditions produce expected error messages, that the various XML configuration files are all set up correctly etc. etc. It seems that Shale has made some attempt to implement some of these unit test features, but actually, the designers of the frameworks should actively make unit testing both possible and easy, and when they produce a reference implementation it should come with a full-fledged mock implementation right out of the box. I might almost say that the mock implementation would be more important than the reference implementation.
Don't get me wrong, I'm not trying to single out JSF here; it just happens to be the framework that finally tipped me over the edge, and now, when I take a breath, I realize that GUI frameworks are not alone in commiting this crime, but they do seem more prone to it and its not just the developers of the frameworks. The developers of the user interfaces that actually use these frameworks are no better. For your average GUI developer, testing seems to consist of having to run the actual application and then working through a test script (script here is used in the same sense as a film-script rather than a shell script, as in it is a written sequence of directions that an actual human being has to perform) to check that it actually works. I'm sorry, but this is the kind of repetitive task that computers are much better at than human beings. You have a good example of a vicious circle here.
On the one hand GUI frameworks have poor to non-existent unit test support. On the other hand most GUI developers wouldn't know a unit test if it hit them in the face. Something needs to break this circle and it has to be the framework developers, after all if you can't write a unit test for your GUI code how can you possibly learn just how useful unit tests are? Oh, and before you bash on me and say that you can't possibly get rid of a human being actually pressing buttons and reading scripts - I agree. But there are many classes of problem (i.e. the ones I listed in the first paragraph of this rant) which are addressed much more easily and completely using unit tests.
You might also point to testing frameworks such as HTTPUnit . These might have their place in integration testing, but one of the benefits of unit testing is that the immediateness of the results shortens the time it takes to fix bugs. In fact, the shorter this time is, the better as research at PAG has shown. You could also point to something like Struts, which has an excellent unit test package called Struts TestCase . OK, this is a good effort, but wouldn't it be better if the developers of Struts actually went out of there way to facilitate unit testing?
So come on guys, when you design the next great GUI framework, make it testable.






