Original Source
Another great piece by Scott Davis on his Mastering Grails series, he now tackles Testing a Grails application. Everyone knows testing is important but when in crunch mode it is a task that is most often than not relegated to another cycle, sometimes it even falls from the schedule/planning and never comes back. Scott has shwon us that doing web application development with Grails is indeed easier and more productive, you can bet the test story in Grails is also improved.
I’m a huge advocate of test-driven development (TDD). Neal Ford (author of The Productive Programmer) says that “it is professionally irresponsible to write code that isn’t tested” (see Resources). Michael Feathers (author of Working Effectively with Legacy Code) defines “legacy code” as any software that doesn’t have a corresponding test associated with it — implying that writing code without tests is an antiquated practice. I often say that a project should have two pounds of test code for every one pound of production code.
Mastering Grails hasn’t yet covered TDD because the series has concentrated until now on ways to take advantage of core Grails functionality. There’s a modest bit of value in testing infrastructure code (that is, code that you didn’t write), but in practice I rarely do it. I trust Grails to render my POGOs to XML correctly, or save my Trip to the database when I call trip.save(). The real value proposition of testing comes into play when you are validating your own custom code. If you write a complex algorithm, you should also have one or more complementary unit tests to prove that the algorithm does what it says it does. In this article, you’ll see how Grails enables and encourages you to test your application.
Grails, being an agile web development framework for the Java platform, comes with a good level of testing support. Every time you create a domain class, controller or service via one the create commands you’ll also get a skeleton test case for that artifact as well, you just need to fill in the blanks. Grails also provides a separation between unit testing and integration testing, which Scott explains very well. And don’t forget that unit tests are executable documentation.
Tags: Grails, Scott Davis