TDD requires a leap of faith

Software, Testing 1 Comment »

This week I did a presentation on Test Driven Development (TDD) at a client to a group of mid to senior level developers that are trying to up-skill in Java. I chose a few academic examples encompassing unit, integration and functional testing and demonstrated ‘live’ on Eclipse how I’d go about developing using a test-first approach. After completing the exercises I showed that with good test coverage I could refactor safely to improve the design.

The response from the audience varied from stunned thoughtful silence to scepticism to plain disbelief. I didn’t get a sense that anyone ‘got it’ to a point where they would go and develop their next class test-first. There was certainly lots of interesting discussion and questions afterwards but I think it all sounded like a bit of a hippy concept to them.

The experience made me reflect back on my adoption of TDD. I’ve been writing tests for years but it wasn’t till about a year ago that I took the leap of faith to a truly test-first approach. The trigger for me was some pairing sessions with experienced developers that showed me the value of it. For years before that I had resisted, in my mind always thinking that there was little difference between test-first and what I was doing.

TDD is a hard concept to sell, it sounds backwards to a developer that has been writing classes forever and has only recently started writing tests at all. Some of the comments I got during the presentation were: ‘I get tired of writing tests, it’s monotonous’, ‘Isn’t it better to write all the code and then give it to someone else to write the tests?’, ‘Wouldn’t TDD end up with a bad overall design since we’re always doing the least we can to make a single test pass?’, ‘We’re always under the pump, all this testing takes too long’. Academic answers to these questions sound shallow because it’s hard to explain what doing good TDD feels like. A few solid pairing sessions would provide a more robust answer to all these questions.

Throughout the presentation I couldn’t help but feel like a bit of a preacher. I know TDD is good, I wouldn’t develop any other way. But how do I qualify the benefits and convince someone that it’s the way to go? In the end, TDD requires a leap of faith. After some experience with it, you won’t turn back.

Selenium page models

Java, Software, Testing No Comments »

Brittle Selenium test suites are a common problem for projects with a significant number of acceptance tests. In the past, Selenese made it really hard to write well factored Selenium tests. The best one could do was write Selenium tests in JSPs and use JSP includes to avoid repetition of command chains (e.g. include Login.jsp).

Selenium RC gives us the ability to leverage the richness of the language to write well factored tests. One pattern that can help here is Page Models. These are essentially an abstraction of the pages in the application using classes. Taking Google as a sample application, we could write simple page models for the home and search results pages.

The page models provide access to page elements and are linked through navigation actions. We can use the page models to write tests that are more intentional and don’t interact with Selenium directly.

For more than one test class we would probably go a bit further and move the setUp and tearDown methods to a common parent or utility class. We could even make the page models reference the Selenium object directly rather than passing it around all the time.

With this approach the application and page models can be changed freely without having an impact on individual tests.

Wary of code reuse

Java, Software No Comments »

This article on InfoQ summarises the way I feel about code reuse.

I’ve worked for a few organisations that have fallen for the code reuse trap. It goes something like this. A smart bunch of well meaning developers write an application. Along their travels, they realise that some of the code they are writing is generic enough to be of use to other projects and thus the common.jar is born (or worse yet, framework.jar). The package ends up being a bucket for common services (e.g. EmailService, AuthenticationService), base classes (e.g. AbstractDomainObject, BaseHibernateDAO), utility classes (e.g. StringUtils, FileUtils) and configuration (e.g. log4j.properties). The project finishes and everyone is happy about having created some reusable code.

Sometime later, along comes another bunch of developers to work on the next great killer application. They notice all the great work done by the first bunch and go about importing common.jar into their project. However, they soon realise that EmailService is missing some features that they need, AbstractHibernateDAO doesn’t work with the new version of Hibernate, etc. So they go about enhancing and adding to common.jar, while carefully managing the dependencies on the first application to make sure they haven’t accientally broken it.

Several months, years and projects later common.jar has grown out of control. There are now 10 projects dependent on it. Making any change to it is a bit of a nightmare, requiring regression testing and sometimes changes to the dependent projects. If projects are running in parallel, common.jar becomes the battleground of merge conflicts. Fear grows and each project now starts avoiding changes to common.jar, which can lead to interesting hacks and workarounds.

Analysis at this point would probably show that any one of the 10 projects only uses about 10% of the capabilities provided by common.jar so the value of the reusable component has been diluted and the pain of managing the dependencies far outweigh any benefit provided by reuse.

So, be wary of code reuse. The cost of building several EmailServices pales into insignificance when compared to inefficiencies introduced by having to manage complex dependencies.

Vendor support for Ruby

Java, Ruby, Software No Comments »

A few weeks ago I did some work for a client that is about to re-platform a public facing website written in Forte 4GL to Java. Forte itself is not the problem, as far as I can tell the technology is sound. Forte is a legacy platform that is no longer supported by Sun. The client is finding it impossible to hire people that have the required skills and want to work on Forte applications. The business wants to make some significant upgrades to the application. Sensibly, the client does not want to invest any more money into a legacy platform so they have kicked off the effort to re-platform it to Java at a considerable cost.

So far, this is an all too common story. There are legacy platforms and applications everywhere. What really surprised me is that the current application is only 4-5 years old, which tells me that Forte went from mainstream to legacy in an amazingly short time.

All this is interesting background in the context of what’s happening with Ruby at the moment. The popularity of Ruby is increasing all the time and we’re finding that corporate clients are starting to choose Ruby over Java more and more. Is this a gamble? Does anyone know where Ruby will be in 5 years time? 10 years? Will the current mob of smart people gathered around Ruby have moved on to the Next Great New Language? I can understand why CIOs are cautious of Ruby, I would be too.

One important consideration is the part played by the open source and Ruby communities. Vendor support is something that corporates traditionally appreciate, it gives them a warm fuzzy reassuring feeling. But in this case Sun bought out Forte and essentially killed it to neutralise a rival to J2EE. Thanks vendor! Ruby is not owned by anyone so the risk of it being exterminated is minimised. If Ruby dies it will be due to natural causes.

Java is an interesting case study because it managed to strike a nice balance between vendor support and a strong community. In the initial stages Sun invested a lot of effort developing, promoting and selling Java despite it’s well documented shortcomings. Sometime in the late 90’s the community around Java really took over and was responsible for making Java mainstream. Java is now owned by the community, just like Ruby.

The case for Ruby in the enterprise could certainly benefit from some vendor support. Efforts to deploy Ruby applications on accepted enterprise platforms will also be key (e.g. JRuby, IronRuby).

© 2007 Tomas Varsavsky, All Rights Reserved. WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Login