Published on ONJava.com (http://www.onjava.com/)
 See this if you're having trouble printing code examples

Using JUnit With Eclipse IDE

by Alexander Prohorenko and Olexiy Prokhorenko



The major provisions of TDD are:


There several different kinds of tests we should consider:



  4. Fix the bug.
  5. Run the test.
  6. Confirm that it passes.










Figure 1
Figure 1. Creating a JUnit test in the Eclipse IDE


The code for TestThatWeGetHelloWorldPrompt.java is as follows:

import junit.framework.TestCase;

public class TestThatWeGetHelloWorldPrompt
    extends TestCase {
    public TestThatWeGetHelloWorldPrompt(
        String name) {
    public void testSay() {
        HelloWorld hi = new HelloWorld();
    public static void main(String[] args) {


To create our own simple test case, we need to follow these steps:
  1. Create an instance of junit.framework.TestCase.
  2. Define tests that return void and whose name begins with the string "test" (such as testWasTransactionSuccessful(), testShow(), etc.).

TestThatWeGetHelloWorldPrompt.java meets both of these criteria: it subclasses TestCase and has a method called testSay(). This method uses an assertEquals() call, which compares the value which we expect to receive against the value returned by say().

The main() method is used to run the tests and present their output. JUnit's TestRunner executes our tests, and provides both graphical and textual output. We use the textual version because that's what Eclipse IDE supports and it's absolutely suitable for us. Once executed, the textual version of the test shows the result as text output and Eclipse IDE uses that to create its own graphic presentation.

So, according TDD provisions, once we run our test we should see that it failed. Let's try. Click Run -> Run as -> JUnit Test (remember that TestThatWeGetHelloWorldPrompt.java should be highlighted in Package Explorer). In the left window, instead of Package Explorer, you will see the JUnit window, which shows a red bar, the failed tests, and details of those failures, as seen in Figure 2. If you do not automatically see it, just click on JUnit label (on the bottom left), which is one of the layers of this screen.

Figure 2
Figure 2. Failed test in JUnit

Perfect! It really fails. Now we can create working code in our project: right-click the ProjectWithJUnit title in the left Package Explorer window, then choose New -> Class. Choose a name for the class — we've assumed it to be HelloWorld. Do not check off any of checkboxes on the bottom of the dialog window, just click Finish. Below is the code for HelloWorld.java:

public class HelloWorld {
    public String say() {
        return("Hello World!");

It's very simple and merits no commentary. So let's test it and see the results. Run our test the same way as described above, and in the left JUnit window you will see a green bar, as seen in Figure 3. The green bar means that our test was successful.

Figure 3
Figure 3. Successful test in JUnit

Now we want to try to make it fail once again, but for a different reason. This will help show how JUnit test covers and reports different errors. Edit the assertEquals() to change the expected return value from "Hello World!" to, for example, "Hello Me!". When you run this JUnit test again, the bar will be red, and at the bottom of the left JUnit window you will see an explanation of what failed, as illustrated by Figure 4.

Figure 4
Figure 4. ComparisonError running JUnit

In conclusion, I would like to mention a few thoughts about testing as a necessary part of the development process. Testing code was always an integral part of any development. But it has been advanced over the last few years, thanks to powerful methodologies (such as "expectations-based development," etc.), accelerated testing toolkits, and the development of better testing processes. If you have found this article interesting, take some time to study formal testing methodology, and use it in your work.



Return to ONJava.com.

Copyright © 2009 O'Reilly Media, Inc.