Java Reference
In-Depth Information
D.3.3
Running JUnit tests from NetBeans
Once you import the projects into your workspace, we can demonstrate how to run
some of the tests there. Before we do that, there are several things to consider.
First, take a closer look at any of the projects. You'll see something like the tree
shown in figure D.10.
These compilation problems are due to the fact that NetBeans, unlike Eclipse,
explicitly keeps two separate directories: one for the source files and one for the
test-source files. For each of these folders NetBeans keeps a different set of libraries
to include in the classpath. As you see in the figure, NetBeans detects src\test\java as
a source folder, and the source-folder classpath probably doesn't contain the JU nit
JAR file. To see all the libraries that are included in the classpath, right-click your
project, choose Properties, and in the box that appears choose the Libraries tag
from the left tree. As you will see, the Compile Tests tab contains the JU nit JAR , so
indeed the compilation problems were caused by the fact that NetBeans recognizes
the folder as a source folder and not as a source-test folder.
To solve this problem, again right-click your project, choose Properties, and in the
Sources tag remove your src\test\java folder from the Source Package Folders list and
add it to the Test Package Folders list. The final setup should look like the one shown
in figure D.11.
To execute a JU nit test from within NetBeans, you need to make sure the test
resides in the correct folder (the test-source folder). Unlike Eclipse, NetBeans will
never execute a JU nit test if it resides in the source folder (if your class resides in the
source folder and you try to execute it, NetBeans will start looking for a main
method instead of running the class as a test). Given that you already made sure all
of your tests are located in the test-source folder, you have several options. Either
right-click inside the class and choose Run File (Shift+F6), or right-click the file
inside the Projects view and select Run File, or simply press Shift+F6. No matter what
method you use, the result should be similar to the screen shown in figure D.12.
Figure D.10
Compilation problems with NetBeans
 
 
Search WWH ::




Custom Search