Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Jump into JUnit 4

Streamlined testing with Java 5 annotations

Andrew Glover (aglover@stelligent.com), President, Stelligent Incorporated
Andrew Glover is president of Stelligent Incorporated, which helps companies address software quality with effective developer testing strategies and continuous integration techniques that enable teams to monitor code quality early and often. Check out Andy's blog for a list of his publications.

Summary:  JUnit 4 has dropped the strict naming conventions and inheritance hierarchies of old, in favor of the streamlined flexibility of Java™ 5 annotations. In this tutorial, a supplement to his popular series on code quality, testing fanatic Andrew Glover shows you how to leverage the new features enabled by annotations, including parametric tests, exception tests, and timed tests. He also introduces JUnit 4's flexible fixtures and shows you how to use annotations, rather than suites, to logically group tests before running them. The tutorial includes several sample tests run in Eclipse and instructions for running JUnit 4 tests in older, incompatible versions of Ant.

Date:  20 Feb 2007
Level:  Intermediate PDF:  A4 and Letter (241 KB | 24 pages)Get Adobe® Reader®

Activity:  54174 views
Comments:  

What else is new?

In addition to the important changes discussed so far, JUnit 4 also has introduced a few minor ones; namely the addition of a new assert method and the elimination of a terminal state.

A new assert

JUnit 4 has added a new assert method for comparing array contents. It isn't a big addition, but it does mean you'll never again have to iterate over the contents of an array and assert each individual item.

For example, the code in Listing 22 isn't possible in older versions of JUnit. This test case fails because of the slight difference in the second element of each array.


Listing 22. assertEquals supports arrays now in JUnit 4
                    
@Test
public void verifyArrayContents() throws Exception{
 String[] actual = new String[] {"JUnit 3.8.x", "JUnit 4", "TestNG"};
 String[] var = new String[] {"JUnit 3.8.x", "JUnit 4.1", "TestNG 5.5"};
 assertEquals("the two arrays should not be equal", actual, var);		
}


No more errors!

A small but sweet change in JUnit 4 is that it eliminates the notion of errors. Whereas previous versions would report both the number of failures and the number of errors, in JUnit 4, a test either passes or it fails.

Interestingly, while one state has been eliminated, a new one has been added, this time because of the ability to ignore tests. When you execute a series of tests, JUnit 4 reports the number of tests run, the number of failures, and the number of tests ignored.

7 of 10 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Java technology
ArticleID=196120
TutorialTitle=Jump into JUnit 4
publish-date=02202007
author1-email=aglover@stelligent.com
author1-email-cc=