Java Reference
In-Depth Information
Figure 3.2
A separate-but-equal filing system
keeps tests in the same package but in
different directories.
Meanwhile, you want the test classes to be able to unit test protected methods, so you
want to keep everything in the same Java package. The solution is to have one package
in two folders. Figure 3.2 shows a snapshot of how the directory structure looks in a
popular integrated development environment ( IDE ).
This is the code for the third chapter, so we used ch03-mastering for the top-level
project directory name (see appendix C). Under the root directory, we created sepa-
rate src/main/java and src/main/test folders. Under each of these, the package struc-
ture begins.
In this case, all of the code falls under the com.manning.junitbook.ch03.mastering
package. The working interfaces and classes go under src/main/java; the classes we
write for testing only go under the src/main/test directory.
Beyond eliminating clutter, a separate-but-equal directory structure yields several
other benefits. Right now, the only test class has the convenient Test prefix. Later you
may need other helper classes to create more sophisticated tests. These might include
stubs, mock objects, and other helpers. It may not be convenient to prefix all of these
classes with Test , and it becomes harder to tell the domain classes from the test classes.
Using a separate test folder also makes it easy to deliver a runtime JAR with only the
domain classes. And it simplifies running all the tests automatically.
JUnit best practices: same package, separate directories
Put test classes in the same package as the class they test but in a parallel direc-
tory structure. You need tests in the same package to allow access to protected
methods. You want tests in a separate directory to simplify file management and
to clearly delineate test and domain classes.
 
 
 
 
 
Search WWH ::




Custom Search