JavaFAQ Home » Java Tools
I'll start with a confession: I'm a unit testing addict. In fact, I just
can't write enough unit tests. If I'm developing for long stretches of time
without having written corresponding unit tests, I get the jitters. Unit tests
give me the confidence that my code works and that I can change it, at a
moment's notice, without the fear of it breaking.
Furthermore, as an addict, I tend to write a plethora of test cases. My high,
however, isn't from writing the test cases; it's in seeing their results.
Consequently, if I can write the tests in a rapid manner, I can view their
results quicker. That way I feel better. Quicker.
Of late, I've been looking to Groovy to appease my unit testing addiction, and
so far I'm impressed. The agility this new language brings to unit testing is
quite exciting and worthy of some serious exploration. In this article, the
first in a new series introducing the practical aspects of Groovy, I'll
introduce you to the pleasures of unit testing with Groovy. I'll start with an
overview of Groovy's unique contributions to development on the Java platform,
then move on to discuss the particulars of unit testing with Groovy and JUnit,
with special emphasis on Groovy's extension of JUnit's TestCase class. I'll
conclude with a working example that shows you, first hand, how to integrate
these groovy features with Eclipse and Maven.
No more Java purism!
Before I launch into the practical aspects of unit testing with Groovy, I
think it's important to talk about the more general issue of its place in your
development toolbox. The fact is, Groovy isn't the only scripting language that
runs on the Java Runtime Environment (JRE), it's just the only one that has been
proposed as a standard language for the Java platform. As some of you will have
learned from the alt.lang.jre series (see Resources), there are myriad options
when it comes to scripting for the Java platform, most of them presenting highly
agile environments for rapid application development.
Despite this abundance of choices, many developers choose to stick with their
favorite and most-familiar paradigm: the Java language. While Java programming
is a fine choice for most situations, there is one very important shortcoming to
wearing Java-only blinders. As a wise person once put it: If the only tool you
have is a hammer, you tend to see every problem as a nail. I think there's a lot
of truth to this saying that is applicable to software development.
Just as I hope to convince you with this series that the Java language is not
and should not be your only choice for developing applications, it's also true
that scripting languages make sense in some scenarios and not in others. What
separates the professional from the tyro is knowing when to apply the power of
scripting and when to eschew it.
For example, scripting is typically not such a good fit for high-performance,
transaction-intensive, enterprise-wide applications; for these cases your best
bet could be a normal J2EE stack. On the other hand, scripting -- and
particularly scripting with Groovy -- can make a lot of sense when it comes to
rapid prototyping of small, highly specific applications that are not
performance intensive, such as configuration systems and/or build systems. It's
also a near-perfect fit for reporting applications and, most importantly, unit
Why unit test with Groovy?
What makes Groovy particularly appealing with respect to other scripting
platforms is its seamless integration with the Java platform. Because it's based
on the Java language (unlike other alternate languages for the JRE, which tend
to be based on earlier predecessors), Groovy presents an incredibly short
learning curve for the Java developer. And once that learning curve has
straightened out, Groovy can offer an unparalleled rapid development platform.
the article with details read here: A simple strategy for unit testing
Java code with Groovy and Junit
On our site more about testing:
Printer Friendly Page
Send to a Friend
Search here again if you need more info!