Basic Eclipse Setup for TDD
Today I like to share my basic setup for TDD with Eclipse.
- 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".
- 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.
- 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:
- 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"
- Add libraries:
- Create a new Folder in your Project ("New -> Folder") called "libs".
- 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.)
- 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.
- Now you should have the following structure in your "Project Explorer":
- Add the libraries to your buildpath:
- Right click on [Your Project] -> "Build Path" -> "Configure Build Path..." and than select the "Libraries"
- 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".
- Right click on [Your Project] -> "Build Path" -> "Configure Build Path..." and than select the "Libraries" tab.
- 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.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)