Unit Testing in Java: How Tests Drive the Code

This short list of frequently asked questions was originally based on the FAQs contained in the JUnit documentation. The official JUnit FAQ list [URL:JUnitFAQ] has completely changed meanwhile. The following questions will be answered:
How do I implement a test case for a thrown exception?
How do I organize my test case classes?
How do I run setup code once for all my test cases?
I get a ClassNotFoundException when I use LoadingTestRunner. What can I do?
Why do I get exception XYZ when using a graphical test runner, but not in the textual test runner?
" assert" has been a keyword since JDK 1.4. Isn't there a conflict with JUnit's assert method?
How do I best integrate JUnit with my favorite development environment?
Catch the exception and if it isn't thrown, call the fail method. Fail signals the failure of a test case. Here is an example:
public void testIndexOutOfBoundsException() { Vector v= new Vector(10) try { Object o= v.elementAt(v.size()); fail("ArrayIndexOutOfBoundsException expected"); catch (ArrayIndexOutOfBoundsException expected) { }}Alternatively, you can use the ExceptionTestCase class:
Make your test case class a subclass of junit.extensions.ExceptionTestCase.
Write the test ignoring exceptions: