This article will show you how to set Eclipse and
your system environment to run JUnit test cases from within Eclipse. It
will also walk you through a local and a remote debugging case.
JUnit has been around for a while and many people
already know how to write and run JUnit tests. The most common
execution environment for JUnit tests is either from a command prompt
or from an Ant script. These two environments provide an unattended
execution support and the latter has even a pretty output formatter
(<formatter>).
JUnit and Eclipse
Eclipse, as nice as it is, also comes with
packaged a JUnit plugin. There is one chief advantage of running JUnit
from Eclipse - debugging. Especially during construction and
transition, debugging JUnit test cases can save considerable time. And
later, when test cases need to be synched with the Java classes, the
debugger can help chase defects much faster than any logger.
Your JUnit test cases will most likely fall into the one of the two major categories:
- Local (Standalone) JUnits - for testing Plain Old Java Objects (POJO) running on a local JVM, i.e. outside a container (J2EE, Web).
- Remote JUnits - test cases calling remote components running inside a container.
Debugging standalone test cases is no different
than debugging any other Java class. Start a local debug process,
attach the debugger and step through the test code.
Remote test cases on the other hand, require a
little more effort. Remote calls get executed in a separate Java
Virtual Machine (JVM), therefore a debug process running in a local JVM
cannot step through the remote code. In the latter case you need to
access the remote code through a socket-based connection. You can
launch the remote JVM with debugging enabled and attach the local
debugger to it. You will have two debug processes in Eclipse - one for
the local JUnit and second for the remote component. Debugging in two
JVMs simultaneously is called parallel debugging.
Figure 1 Local and Remote Debugging
Your First JUnit Test Project
This section will show you how to create a Java project for your test cases.
- First, create a new Java project. This project will hold your local JUnit code.
- Create a test case. For this, create a standalone Java class and extend JUnit's TestCase.
public class YourTest extends TestCase {
public YourTest(String name) {
super(name);
}
protected void setUp() { }
protected void tearDown() { }
public void testMethod1() {
// Your code here...
assertTrue(true);
}
public void testMethod2() {
// Your code here...
assertTrue(false);
} };
- Build the project.
Debugging Local JUnit Tests
To debug local JUnits you need to open a standalone test case and launch a debug process.
- In Eclipse's Debug perspective, open any JUnit test case
- From the menu select Run | Run As | JUnit Test
- Observe the new JUnit view opens. If the test is successful the progress bar turns green, otherwise it turns red.
- In the JUnit view, click the Hierarchy tab. Expand the test case methods. Observe each
test method (See Figure 2 Running
a JUnit test case from Eclipse. The progress bar turns green if the
test completes with no errors or failures. Notice the Hierarchy tab and
the individual test methods).
- Now set a breakpoint in any of the test methods.
- From the menu select Run | Debug As | JUnit Test
- Step through the test case

Figure 2
Running a JUnit test case from Eclipse.
The progress bar turns green if the test completes with no errors or failures.
Notice the Hierarchy tab and the individual test
methods under it
Debugging Remote JUnit Tests
Remote JUnits consume the services of remote
components like EJBs or web services. These components reside in remote
containers (JVMs) and the test cases use RMI/IIOP to talk to them. When
the local JUnit initiates a remote call to a component outside the
local JVM, the debug process will momentarily pause until the remote
call is completed. In order for the debugger to continue in the remote
JVM, it needs to connect to it through a debug socket. The remote JVM
needs to be launched in debug mode (See this article
on how to start a remote process with debugging options enabled). After
that you need to connect to the remote debugger from Eclipse and only
then will the debugger be able to switch between the local and remote
stacks.
The following steps describe how to launch a local
and then a remote debug session and how to attach the Eclipse debug
processes to them. The assumption is that you will be testing a remote
component running inside a WebLogic server instance.
- Edit the WebLogic launch script to add the debug options.
Follow the steps in this article to make the necessary changes.
- Launch WebLogic with debugging enabled
- In Eclipse, create a remote debug process.
Follow the above document for the steps necessary to create and attach
to the remote debugging process.
- Launch the remote debugger. You should see a debug process in the Debug view
- Open the remote component and set a breakpoint
- In Eclipse's Debug perspective, open your JUnit test case
- Set a breakpoint anywhere in the test case
- From the menu select Run | Debug As | JUnit Test
- Start stepping through the test case code.
When the test case makes a remote call the debugger will automatically
jump to the remote debug process. When the code execution in the remote
component finishes the debugger will return to the local test case
debug. See Figure 3 Parallel
debugging in Eclipse. Notice the two separate debug processes in the
Debug view. The first one is the remote and the second one is the local
debug process. Switching between debug processes is automatically
handled by Eclipse for more details.

Figure 3 Parallel debugging in Eclipse. Notice the two separate debug processes in the Debug view.
The first one is the remote and the second one is the local debug
process. Switching between debug processes is automatically handled by
Eclipse
System Information:
- Windows XP Pro
- JDK 1.4.2_03
- Eclipse 3.2
- JUnit 3.8.1