Java Reference
In-Depth Information
How do you get started using JUnit ? All that's necessary is to write a test. Here I have writ-
ten a simple test of my Person class and placed it into a class called PersonTest (note the
obvious naming pattern):
public
public class
class PersonTest
PersonTest {
@Test
public
public void
void testNameConcat () {
Person p = new
new Person ( "Ian" , "Darwin" );
String f = p . getFullName ();
assertEquals ( "Name concatenation" , "Ian Darwin" , f );
}
}
To run it manually, I compile the test and invoke the command-line test harness TestRunner :
$ javac PersonTest.java
$ java -classpath junit4.x.x.jar junit.textui.TestRunner testing.PersonTest
.
Time: 0.188
OK (1 tests)
$
In fact, running that is tedious, so I usually have a regress target in my Ant scripts. There is a
junit task in Ant's “Optional Tasks” package. [ 7 ] Using it is easy:
<target name="regress" depends="build">
<junit>
<test name="PersonTest" />
</junit>
</target>
In fact, even that is tedious, so nowadays I just put my tests in the “standard directory struc-
ture” (i.e., src/test/java/ ) with the same package as the code being tested, and run Maven (see
Automating Dependencies, Compilation, Testing, and Deployment with Apache Maven ) ,
which will automatically compile and run all the unit tests, and halt the build if any test fails.
 
Search WWH ::




Custom Search