Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Get Acquainted with the New Advanced Features of JUnit 4 : Page 4

Learn how to migrate from JUnit 3.8 to JUnit 4. Discover version 4's new features, including extensive use of annotations, and find out the status on IDE integration.




Application Security Testing: An Integral Part of DevOps

Tools Integration (or Lack Thereof)
As I'm writing this article, JUnit 4 integration in IDEs is not yet perfect. In fact if you try to run the test classes we’ve just seen, they will not work in any IDE because they are not recognized as being test classes. For forward compatibility JUnit 4 comes with an adapter (junit.framework.JUnit4TestAdapter) that you have to use in a suite() method. Here is the code you have to add in every class to make them understandable by IDEs, Ant, and JUnit 3.8 text runner:

public static junit.framework.Test suite() { return new JUnit4TestAdapter(CalculatorTest.class); }

Intellij IDEA
IDEA 5 does not integrate JUnit 4. We will have to wait for IDEA 6. In the meantime I've used the early access version (Demetra build 5321). The parameterized test case didn't work. Figure 1 shows the CalculatorTest executing (the ignored test is represented with a different icon).

Figure 1. IDEA Demetra is only running CalculatorTest.
Figure 2. Eclipse 3.2RC7 is running the suite class AllCalculatorTests.

I’ve used the version 3.2 RC7 of Eclipse. It is not a stable version but the integration with JUnit 4 is much better than with IDEA. Figure 2 shows what you get when running the AllCalculatorTests class.

As you can see, the parameterized test case (SquareTest) is represented as seven individual tests.

Ant Integration
The junit task currently only supports JUnit 3.8 style tests, meaning you also have to wrap your JUnit 4 tests in a JUnit4TestAdapter in order to run them in Ant. The <junit> task is used in exactly the same way as in JUnit 3.8:

<!-- Test --> <target name="test" depends="compile"> <junit fork="yes" haltonfailure="yes"> <test name=" junit4.AllCalculatorTests"/> <formatter type="plain" usefile="false"/> <classpath refid="classpath"/> </junit> </target>

JUnit: Losing Market or Coming Back Strong?
For a long time JUnit was the defacto unit testing framework. But lately not much has happened to this framework: no major release, no notable new features. This is possibly the reason why other frameworks, such as Test-NG have started taking over.

With this new version, JUnit is back on track. It has new APIs and now uses annotations, making it easier to develop test cases. In fact, the JUnit developers have started to think of new, future annotations. For example, you could add a @Prerequisite annotation for a test case that depends on prerequisites (e.g. you need to be online to execute this test); or add a @Repeat annotation that would specify the number of repetitions along with a timeout (e.g. repeat a test five times to make sure there is a real timeout problem); or even add a platform parameter to the @Ignore annotation (e.g. @Ignore(platform=macos), which would ignore a test only if you run on a MacOS platform). As you can see, JUnit is still alive with a promising future.

Antonio Goncalves is a senior architect specialized in Java/J2EE. Former BEA consultant he now helps insurance, finance and telecommunication clients set up their architectures. He also teaches J2EE at CNAM University in Paris.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date