Home > Software Quality Tips > Software Testing > Building beautiful software tests around JUnit
Software Quality Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

SOFTWARE TESTING

Building beautiful software tests around JUnit


Alberto Savoia
09.17.2007
Rating: -4.00- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


The following is an excerpt from Chapter 7, "Beautiful Tests," taken from the book Beautiful Code.


When speaking of beautiful tests, it's hard not to think of the JUnit testing framework. Because I'm using Java, deciding to build my beautiful tests around JUnit was a very easy decision. But before I do that, in case you are not already familiar with JUnit, let me say a few words about it.

JUnit is the brainchild of Kent Beck and Erich Gamma, who created it to help Java developers write and run automated and self-verifying tests. It has the simple, but ambitious, objective of making it easy for software developers to do what they should have done all along: test their own code.

Unfortunately, we still have a long way to go before the majority of developers are testinfected (i.e., have experimented with developer testing and decided to make it a regular and important part of their development practices). However, since its introduction, JUnit (helped considerably by eXtreme Programming and other Agile methodologies, where developer involvement in testing is nonnegotiable) has gotten more programmers to write tests than anything else.* Martin Fowler summed up JUnit's impact as follows: "Never in the field of software development was so much owed by so many to so few lines of code."

JUnit is intentionally simple. Simple to learn. Simple to use. This was a key design criterion. Kent Beck and Erich Gamma took great pains to make sure that JUnit was so easy to learn and use that programmers would actually use it. In their own words:

So, the number one goal is to write a framework within which we have some glimmer of hope that developers will actually write tests. The framework has to use familiar tools, so that there is little new to learn. It has to require no more work than absolutely necessary to write a new test. It has to eliminate duplicate effort.†

The official getting-started documentation for JUnit (the JUnit Cookbook) fits in less than two pages

Here's the key extract from the cookbook (from the 4.x version of JUnit):

When you need to test something, here is what you do:

  1. Annotate a method with @org.junit.Test
  2. When you want to check a value, import org.junit.Assert.* statically, call assertTrue( ), and pass a Boolean that is true if the test succeeds

For example, to test that the sum of two Moneys with the same currency contains a value that is the sum of the values of the two Moneys, write:

    @Test
    public void simpleAdd( ) {
      Money m12CHF= new Money(12, "CHF");
      Money m14CHF= new Money(14, "CHF");
      Money expected= new Money(26, "CHF");
      Money result= m12CHF.add(m14CHF);
      assertTrue(expected.equals(result));
    }

If you have any familiarity with Java, those two instructions and the simple example are all you need to get started. That's also all you need to understand the tests I will be writing. Beautifully simple, isn't it? So, let's get going.

>> Read the complete chapter, "Beautiful Tests."


Rate this Tip
To rate tips, you must be a member of SearchSoftwareQuality.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Software Testing
Free Web proxy security tools software testers should get to know
Best practices for Scrum and when to apply them
How to deal with iteration issues in Agile
Five steps to fostering better software tester and QA results
How to stop developer vs. tester, quality-killing blame game
Easing software performance testing and usability modeling pressures
How to apply modeling techniques to support software testing
Calculating mean time to failure in performance testing
The lowdown on PCI compliance
5 ways to answer executives' unfair software test, QA questions

Software testing tools and frameworks
Performance testing tools - Commercial, less expensive and free
Software Testing Ezines
New IBM Rational, Tivoli integrated tools pair development with IT
STPCon: Do reality checks on performance test products, panelists advise
Demo: Using WebGoat, a free software testing tool
Getting answers about OpenSTA script problems
Defining core software regression tests
Selecting the best tool for stress and load testing
Required prerequisites for performance testing
Surgient 7's self-provisioning promises software testers quick IT resource access

Software unit testing
Software testing deliverables: Developing a software testing strategy
Evaluating the benefits of automated software testing
Adopting continuous integration brings agility, other benefits
Tools, standards address persistent quality assurance (QA) issues
Increasing productivity with unit testing
Don't write simplistic test cases
How to develop a checklist for unit, integration and system testing
Making unit testing a priority
The Art of Debugging with GDB, DDD, and Eclipse -- Ch. 1
An approach to integration testing

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
JUnit  (SearchSoftwareQuality.com)
NUnit  (SearchSoftwareQuality.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Software Design & Testing - Project Management
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2006 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts