I'm a Java Developer for ERP Systems at beeline-group cologne. Christoph has posted 2 posts at DZone. View Full User Profile

Basic Eclipse Setup for TDD

  • submit to reddit

Today I like to share my basic setup for TDD with Eclipse.

  1. Get Eclipse:
    I'm sure you know where you get Eclipse but just for the complete picture you can download it here: http://www.eclipse.org/downloads/. I prefer the "Eclipse IDE for Java EE Developers".
  2. Install and start Eclipse:
    • After you downloaded the compressed IDE, just extract it in your preferred program folder and lunch the executable.
    • Select your workspace folder:
    • After you have closed the "welcome" tab you are ready to start.
  3. Install the continuous test runner:
    I use infinitest as a continuous test runner. It is a plug-in for eclipse which continuously runs the JUnit tests that are relevant to the files I just changed. So I get immediate feedback and have not to interrupt my flow for test execution.
    There are install instructions on the infinitest page so not go to repeat them here.
    After the installation you see the infinitest bar in the lower right corner of eclipse:
  4. Create the project:
    • Now it is time to create your project. Just click on "File -> New -> Project..." and select "Java Project" in the "New Project" wizard. Click on "Next".
    • Enter a "Project name" in the wizard and click "Finish"
  5. Add libraries:
    1. Create a new Folder in your Project ("New -> Folder") called "libs".
    2. I use easyMock as mocking framework.
      • The latest version can be downloaded on the easyMock download page.
      • The downloaded archive file contains the lib and documentation, etc. You only need the easymock-x.x.jar file. Copy it to your just created "libs" folder.
        (On you filesystem you find it in [Your Project] folder located in the workspace folder you selected at eclipse start.)
    3. Hamcrest provides a lot of usefull matcher objects for JUnits assertThat (you know assertThat, right?) and mock expectations.
      • Download the latest (stable) hamcrest-all-x.x.jar from the download list and store the file in your "libs" folder.
    4. Now you should have the following structure in your "Project Explorer":
    5. Add the libraries to your buildpath:
      • Right click on [Your Project] -> "Build Path" -> "Configure Build Path..." and than select the "Libraries" tab.
      • Click on "Add JARs..." and select the two jar files in your "libs" folder.
      • Close the "JAR Selection" with a click on "OK".
      • Click on "Add Library..." choose "JUnit" and click on "Next". Then select "JUnit 4" and close the dialog with "Finish".
      • Close the "Java Build Path" Config Dialog with click on "OK".
  6. Add favorites for static types:
    With favorites for static members or types the content Assist will propose the favorite members or members of the favorite types and add the static import automatically.
    You find the configuration on: "Window -> Preferences -> Java -> Editor -> Content Assist -> Favorites".
    I use to add the types: "org.junit.Assert.*", "org.hamcrest.Matchers.*" and "org.easymock.EasyMock.*".

Thats it. You can start to write your first JUnit TestCase.

Ok in general I use Maven for the dependencies and also add a version control system.

Your rating: None
Published at DZone with permission of its author, Christoph Pater.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


David Grant replied on Fri, 2011/04/08 - 2:31am

The first two screenshots in this article are distorted. Could you update them please?

Christoph Pater replied on Fri, 2011/04/08 - 3:40am in response to: David Grant


Puneet Monga replied on Fri, 2011/04/08 - 2:08pm

Also mention moreunit plugin in your article. TDD will be even more fun with it.

Christoph Pater replied on Sat, 2011/04/09 - 1:36pm in response to: Puneet Monga

Thanks for the tip. Will evaluate it and decide to add it.

James Ervin replied on Tue, 2011/04/26 - 6:19pm

You know EasyMock is nice and all but you really should be using Mockito.

Liezel Jane Jandayan replied on Mon, 2011/08/22 - 9:02pm

Eclipse is a Java program, but it uses a custom user interface toolkit that does not run on all platforms that supports Java 2. Check the web site for supported platforms. Eclipse requires a Java 2 runtime, so you need to install the Java 2 SDK first before installing Eclipse.Yochanan Berkowitz

Niko Miko replied on Tue, 2011/09/20 - 6:12am

I’ve read through a number of the articles in your website , and I love the way you blog. I included it to my favorites blog site list and will also be checking quickly. Cheap Adele Tickets

Christoph Pater replied on Thu, 2011/12/01 - 3:39pm in response to: Christoph Pater

I checked moreunit but I can't utilize it for TDD. It only supports one testMethod per method to check with a coressponding name.

David Harkness replied on Sat, 2012/10/27 - 6:16pm

Great getting-started article. Could you add a few links to some TDD 101 articles?

David Harkness replied on Sat, 2012/10/27 - 6:14pm in response to: Christoph Pater

MoreUnit has an option to consider any test that calls a method as covering that method. Check the Enable extended search for test methods option in the preferences.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.