May 14, 2009

Switch between class and test in NetBeans

When we have a Java class in our NetBeans project with a corresponding JUnit test class we can easily switch between them. When we are editing our Java class file and want to go to the test class we select Navigate | Go to Test (Ctrl+Shift+T in Windows). NetBeans jumps to our test class!

To go the other way, we must have our test class open. Now we select Navigate | Go to Tested Class (Ctrl+Shift+T in Windows) and NetBeans jumps to the Java class.

NetBeans assumes the name of the test class starts with the Java class name we want to test, and the package in which both the test class and Java class reside are the same. So for example if we have a Java class com.mrhaki.netbeans.Sample, the test class must be com.mrhaki.netbeans.SampleTest. The files don't have to be in the same directory structure. So the sources can be in a src/ directory and the test classes in a test/ directory.