Java Reference
In-Depth Information
files created through the
Project
window. Using templates, you can start off with
your basic class framework already defined, rather than an empty document.
7.2.1
Creating a test case from a file template
For our example template, we have followed the canonical
JU
nit test case struc-
ture and the optional constructor, as well as stubbing out the fixture methods.
The complete text of our
JU
nit file template is shown in listing 7.2. Of course, you
may prefer a slightly different layout, so feel free to customize it to your liking.
Listing 7.2
A file template for producing JUnit tests
#parse("File Header.java")
package ${PACKAGE_NAME};
import junit.framework.TestCase;
public class ${NAME} extends TestCase {
public ${NAME}(String test) {
super(test);
}
/**
* The fixture set up called before every test method.
*/
protected void setUp() throws Exception {
}
/**
* The fixture clean up called after every test method.
*/
protected void tearDown() throws Exception {
}
public void testSomething() throws Exception {
}
}
As you can see, this template covers all the basic structure required by the
JU
nit
API
. All you have to do now is write your test methods, starting with adding some
logic to the
testSomething()
stub, which as written doesn't accomplish a heck of a
lot (but at least it always passes!).
7.2.2
Adding the JUnit library to your Classpath
IDEA
ships with the latest version of the
JU
nit
JAR
file, but by default it isn't
included in your project's Classpath. This causes
IDEA
to display import failure