Java Reference
In-Depth Information
Figure 2.1
JUnit's green bar, shown in Eclipse
2.4
Composing tests with a suite
For a simple test, you can compile the simple calculator test program from listing 2.1
and hand it to the console façade runner, like this:
>java org.junit.runner.JUnitCore CalculatorTest
The test should run fine assuming the classpath is configured properly. This is simple
enough—at least as far as running a single test case is concerned.
2.4.1
Composing a suite of test classes
The next step is to run more than one test class. To facilitate this task
JU
nit provides
the
test
Suite
.
The
Suite
is a container used to gather tests for the purpose of group-
ing and invocation.
JU
nit designed the
Suite
to run one or more test cases. The test runner launches
the
Suite
; which test case to run is up to the
Suite
.
You might wonder how you managed to run the example at the end of chapter 1,
when you didn't define a
Suite
. To keep simple things simple, the test runner auto-
matically creates a
Suite
if you don't provide one of your own.
The default
Suite
scans your test class for any methods that you annotated with
@Test
. Internally, the default
Suite
creates an instance of your test class for each
@Test
method.
JU
nit then executes every
@Test
method independently from the others to
avoid potential side effects.