There are a number of different ways that you can start a JUnit process from within Workshop. The actual method you use is not particularly relevant, although some may be more or less convenient depending on your application’s setup and usage. Of course, you can also start JUnit separately from the command line if you prefer.
Regardless of the method used to start JUnit, it will create a .junitsession file in your home directory to remember which tests you have previously run. Because you won’t have to retype the test class’ name after you have run it for the first time, it’s less important that JUnit be passed the test class’ name on startup, although it’s still more convenient to run a test directly instead of first starting JUnit and then selecting the test from the combo box.
Sections in this topic include:
If JUnit tests are saved in a Java project, you can configure WebLogic Workshop to use the start button to launch your tests. You must give the test class a main() method that starts up the JUnit runner of your choice. If you have not already configured the debugger settings for the project, click the Start button and Workshop will ask you if you would like to set the current class as the main class. Alternatively, go to Tool-->Project Properties…-->[Test project], and select Debugger from the list. Be sure that the Create new process radio button is selected and fill in the name of your test class.
Assuming you want to turn off JUnit’s reloading classloader, your main() method might look something like:
String[] jUnitArgs = new String[] { "-noloading", TestCase.class.getName()}; junit.swingui.TestRunner.main( jUnitArgs );
Assuming you have your classpath set up to include all runtime dependencies, you will be up and running. This will also let you debug code running in the JUnit process, which is often convenient for tracking down why a test is failing.
Workshop provides a means for launching third-party programs through the IDE. Go to Tools-->IDE Preferences… and select the Tools node in the list. You can create a new tool configuration to launch JUnit. Just set the full command-line used to start JUnit, including a –classpath argument, and give it a sensible directory in which to start the process. For example, if your application is rooted in c:\wlw_app and you have installed WebLogic Workshop in the c:\bea directory, you might use c:\wlw_app as the directory. Assuming that you’re testing code in a Java project named JavaProject and your test code is in a separate Java project named UnitTestProject and you have defined a TestSuite in a class named somePackage.MainTestSuite, your command would look like something of the form:
c:\bea\jdk141_05\java.exe –classpath c:\wlw_app\APP-INF\lib\junit-3.8.1.jar; c:\wlw_app\APP-INF\lib\UnitTestProject.jar; c:\wlw_app\APP-INF\lib\UnitTestProject.jar; junit.swingui.TestRunner –noloading somePackage.MainTestSuite
You can then easily launch JUnit by going to the Tools-->External Tools-->[JUnit Tool Name] menu in the IDE.
Ant provides an optional task for running JUnit tests, the <junit> task. A call to Ant might look as follows:
<target name="test"> <junit haltonerror="yes" fork="true"> <test name="JPFTest" /> <classpath> <pathelement location="${app.local.directory}/APP-INF/lib/junit-3.8.1.jar"/> <pathelement location="${app.local.directory}/APP-INF/lib/JPFTestProject.jar"/> <pathelement location="${app.local.directory}/APP-INF/lib/htmlunit-1.2.3.jar"/> <pathelement location="${app.local.directory}/APP-INF/lib/nekohtml-0.7.7.jar"/> <pathelement location="${app.local.directory}/APP-INF/lib/commons-logging-1.0.3.jar"/> <pathelement location="${app.local.directory}/APP-INF/lib/commons-httpclient-2.0.jar"/> </classpath> </junit> </target>
If you launch JUnit through either the External Tools or Ant methods, you can still debug it by attaching to the JUnit process from the IDE. First, create or choose a Java project inside your Workshop application. It does not necessarily need to contain the code for your tests. Go to its Project Properties and configure the debugger settings to attach to a port of your choosing on the machine that will be running the JUnit tests. Then, start the JUnit process, passing its Java Virtual Machine the correct arguments to start listening for a debugger to attach, typically something of the form, where SELECTED_PORT is replaced with the port on which it should listen for the debugger:
-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=SELECTED_PORT,suspend=y,server=y
Although the virtual machine will start, it will not begin running the tests until a debugger attaches. In the IDE, open a document in the Java project for which you configured the debugger settings and hit the Start button. The debugger should quickly attach to the process and JUnit should start executing the tests.
You can also use Ant to generate reports based on your test results. For more information on how to integrate Ant and JUnit, go to the Optional Tasks sections of the Ant Tasks topic in the Ant manual.
By default, JUnit’s Swing interface uses a classloader that automatically reloads classes before running tests. This is convenient because you don’t need to restart JUnit after recompiling your classes. However, you might have problems with the way the classloader works in some cases, causing spurious ClassCastExceptions and other unwanted problems. When launching JUnit from a script, through Ant, or from the External Tools, you can disable the special classloader by passing in the –noloading argument. If you are using a main() method inside your test class, you can accomplish the same thing with the following code, where YourTestClass has been replaced with the name of the test class:
public static void main( String[] args ) { String[] jUnitArgs = new String[] { "-noloading", YourTestClass.class.getName() }; junit.swingui.TestRunner.main( jUnitArgs ); }