Running JUnit
Problem
You want to run your tests.
Solution
We have already demonstrated how to run JUnit using Ant, back in
Chapter 3. In order to run tests from a script or
in an IDE, include junit.jar in your classpath
and then use the junit.textui.TestRunner class to
run your tests in text mode. Use
junit.swingui.TestRunner
to run the tests in a Swing
GUI.[20]
Discussion
JUnit can run tests in text or graphical mode. Text mode is faster, and is excellent for running tests as part of an automated build process. Graphical tests are more interesting to run, and can make it easier to analyze output from a large number of tests.
Text testing
Here’s an
example
session using the text-based TestRunner. The first
line is typed at the prompt; the rest is output. The
TestPerson class is the test case from the
previous recipe.
java junit.textui.TestRunner com.oreilly.javaxp.junit.TestPerson .F.F Time: 0.02 There were 2 failures: 1) testGetFullName(com.oreilly.javaxp.junit.TestPerson)junit.framework. AssertionFailedError: expected:<Aidan Burke> but was:<AidanBurke> at com.oreilly.javaxp.junit.TestPerson.testGetFullName(C:/cvsdata/java_xp_ cookbook/examples/src/com/oreilly/javaxp/junit/TestPerson.java:24) 2) testNullsInName(com.oreilly.javaxp.junit.TestPerson)junit.framework. AssertionFailedError: expected:<? Burke> but was:<?Burke> at com.oreilly.javaxp.junit.TestPerson.testNullsInName(C:/cvsdata/java_xp_ cookbook/examples/src/com/oreilly/javaxp/junit/TestPerson.java:29) FAILURES!!! Tests run: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access