Java Reference
In-Depth Information
Figure 1.1
Execution of the JUnit distribution sample tests using the text test runner
Windows
java -cp junit-4.6.jar;. junit.samples.AllTests
UNIX
java -cp junit-4.6.jar:. junit.samples.AllTests
The
AllTests
class contains a
main
method to execute the sample tests:
public static void main (String[] args) {
junit.textui.TestRunner.run(suite());
}
Figure 1.1 shows the result of the test execution.
Notice that the
JU
nit text test runner displays passing tests with a dot. Had there
been errors, they would have displayed with an
E
instead of a dot.
In part 3 of the topic, we look at running tests using the Ant build tool and also the
Maven build tool.
1.6
Testing with JUnit
JU
nit has many features that make it easy to write and run tests. You'll see these fea-
tures at work throughout this topic:
Separate test class instances and class loaders for each unit test to avoid
side effects
■
JU
nit annotations to provide resource initialization and reclamation methods:
@Before
,
@BeforeClass
,
@After
, and
@AfterClass
■
A variety of assert methods to make it easy to check the results of your tests
■
Integration with popular tools like Ant and Maven, and popular
IDE
s like
Eclipse, NetBeans, IntelliJ, and
JB
uilder
■
Without further ado, let's turn to listing 1.4 and see what the simple
Calculator
test
looks like when written with
JU
nit.