Java Reference
In-Depth Information
Listing A.8
Timeout parameter in the
@Test
annotation
public
class
CalculatorTest {
@Test(timeout=5000)
public
void
testSomethingTimeConsuming() {
[...]
}
}
A.4
New JUnit runners
This section briefly covers some of the new
JU
nit runners included in
JU
nit 4.x.
A.4.1
Test runners
The 3.x version of
JU
nit comes with Swing and
AWT
test runners that are no longer
part of the
JU
nit distribution. The test runner façade that you can use to start your
tests from the console is now called
org.junit.JUnitCore
.
With no
GUI
test runners included in the distribution, the only way to glimpse the
old green bar is to use your favorite
IDE
; they all have
JU
nit 4.x support.
A.4.2
Test suites
The old way of constructing sets of your tests involved writing a
suite()
method and
manually inserting all the tests that you want to be present in the suite. Because the
new version of
JU
nit is annotation oriented, it seems somehow logical that the con-
struction of suites is also done by means of annotation. Further, the suite construction
is done not by one annotation but by two annotations: the
@RunWith
and
@Suite-
Classes
annotations.
The first annotation lets you define test runners that you can use to run your tests.
The
@RunWith
annotation accepts a parameter called
value
, where you need to specify
the test runner to run your suite:
Suite.class
. This test runner is included with
JU
nit,
along with some other runners. But in this annotation you can also specify a custom
runner. You can find out how to implement your own
JU
nit runners in appendix B.
The second annotation declares all the tests that you want to include in the suite.
You list the classes that hold your tests in the
value
parameter of the
@SuiteClasses
annotation.
Listing A.9 shows how to construct test suites in
JU
nit 4.x.
Listing A.9
Constructing test suites with JUnit 4.x
@RunWith(value=Suite.
class
)
@SuiteClasses(value={CalculatorTest.
class
, ComputerTest.
class
})
public
class
CalculatorTests {
[...]
}
Some people find the way test suites are done in
JU
nit 4.x unnatural, and indeed, at
first sight it is. But once you start writing tests, you'll see that there's nothing unnatural