«    »

Java Unit Testing Tutorial: Initial Setup

In this section of the Java Unit Testing Tutorial you will install JUnit, the unit testing framework you will use for unit testing, configure Eclipse to run your project's tests, and finally configure your project for the unit test code you will write.

Previous: Tutorial Introduction Next: Writing Your First Unit Test

Install JUnit

Follow these steps to install JUnit:

  1. Download Junit. You just need the individual jar file, but you may want to grab the zip file that also includes the source code and documentation..
  2. Place the junit jar file on the classpath for your project:
    1. Create a directory called "libs" under your project root directory.
    2. Copy the file "junit-4.5.jar" you downloaded into this directory.
    3. Ensure that the directory and jar file are visible within Eclipse. If not, right-click on the project and select "Refresh" to synchronize Eclipse with the underlying file system.
    4. Within Eclipse navigate to the junit jar file, right-click on it, and choose "Build Path | Add to Build Path".

Configure Eclipse

Follow these steps to configure Eclipse to run your project's tests:

  1. Within Eclipse right-click on the project and choose "Run As | JUnit Test". A dialog should appear with the message "No tests found with test runner 'JUnit 4.0'.". This message appears because we have not yet created any unit tests. You will fix that in the next section of this tutorial. Despite the message, performing this action created the run configuration.
  2. Right-click again on the project and choose "Run As | Run Configurations...". This will display the "Run Configurations" dialog box that allows you to edit and create run configurations. On the left side is a tree listing all the run configurations. Under the node "JUnit", click on the run configuration with a name corresponding to the name of your project. (This will be "UnitTestingTutorial for our example.) Note that under the tab "Test" on the right it indicates that this configuration will "Run all tests in the selected project".
  3. Select the tab "Common", and within the section "Save As" choose the option "Shared file". This creates a run configuration file within your project which you can check into version control and thus make available to the rest of your team. By default this launch file will be saved to your project's root directory. Within the section "Display in favorites menu" select the option "Run". Click the button "Apply" to save your changes, then select "Close".
  4. Check your project's root directory to confirm that the run configuration file was created. It will be named the same as your project name in Eclipse with an extension of ".launch".
  5. Within Eclipse expand the favorites menu of the Run button in the Toolbar as per the image below. The top entry of this menu should correspond to the run configuration you just made - it should be named the same as your project with a JUnit icon. Now ignore the menu and click the Run button. This will run the top entry of the menu, executing the project's unit tests. The message that no tests were found should appear again.

Eclipse Run Favorites Menu

Configure Your Project

Follow these steps to configure your project for the unit test code you will write:

  1. Create the directory structure "src/test" under the root directory of your project. This is where the unit test code you will write will go. In Eclipse right-click on this "test" directory and select "Build Path | Use as Source Folder".
  2. Create the directory structure "src/main" under the root directory of your project. This is where the actual project code should go. In Eclipse right-click on this "main" directory and select "Build Path | Use as Source Folder". If your existing project has the source code under a different directory, move it under "src/main", right click on the other directory and choose "Build Path | Remove from Build Path".

This concludes the initial setup. You can now execute tests for your project, but without any tests this is not too interesting. Fortunately you are now ready to create your first unit test and this is the topic of the next section of this tutorial.

Previous: Tutorial Introduction Next: Writing Your First Unit Test

If you find this article helpful, please make a donation.

«    »