Java Reference
In-Depth Information
A.1.1
JDK required
JU
nit 4 uses a lot from Java 5 annotations, generics, and static import features.
Although the
JU
nit 3.x version can work with
JDK
1.2+, this usage requires that the
new version of
JU
nit be used with Java 5 or higher.
A.1.2
Backward/forward compatibility
All of the
JU
nit 4.x test runners are able to execute
JU
nit 3.x tests with no modification
whatsoever. But what about executing
JU
nit 4.x tests with the
JU
nit 3.x test runner?
Now this is a trickier one, but it's absolutely necessary to support so that external tools
such as Ant or Eclipse can work with
JU
nit without needing an update.
The key is to use the
JUnit4TestAdapter
and wrap all of your tests like the one
shown in listing A.1.
Listing A.1
JUnit4TestAdapter
to wrap your JUnit 4.x tests
[...]
import
junit.framework.JUnit4TestAdapter;
public
class
TestJUnit4Tests
extends
TestCase {
public
static
junit.framework.Test suite() {
return
new
JUnit4TestAdapter(SampleTest.
class
);
}
}
It's that simple. At
B
you need to pass your
JU
nit 4.x tests to the
JUnit4TestAdapter
,
and they immediately get converted to
JU
nit 3.x tests so that they can be run with the
JU
nit 3.x runners.
B
A.2
Changes in the API
The changes listed here concern the inner structure of
JU
nit: all the new features
added to the
API
that we need to know.
A.2.1
Package structure
The new version of
JU
nit is built on the idea of backward compatibility. The develop-
ers wanted to make sure that you, as a software developer, are able to execute any
JU
nit 3.x test case with the
JU
nit 4.x library in your classpath. That's why they included
all the new features in a new package,
org.junit
. The old package,
junit.framework
,
is also bundled in the distribution.
A.2.2
Constructors
If you're maintaining tests written prior to
JU
nit 3.8.1, your class needs a
String
con-
structor, for example:
public
CalculatorTest(String name) {
super
(name); }
This is no longer required with
JU
nit 3.8.1 and later.