How to set up JUnit in Eclipse

How to set up JUnit in Eclipse


JUnit simplifies the Unit testing process in Java with visual indicators and a Unit testing framework. JUnit use in the Eclipse IDE is convenient and quick to configure.




Many versions of Eclipse will come with JUnit already included. Versions not meant for Java may not, however. When Eclipse is opened, a quick check to see if JUnit was included is to go to:

File -> New -> JUnit Test Case

If this option is there, JUnit was included. If not, you can instead select:

Help -> Install New Software

within Eclipse. In the Work with: field, your Eclipse version should have a Site entry in the dropdown, such as:

Once you choose this, you can then navigate to:

Programming Languages -> Eclipse Java Development Tools

in the table below and select that option.

Once you installed the JUnit software, you can start creating tests for your Java classes, such as by going to:

New->JUnit Test Case

on a .java file’s context menu in Package Explorer.

Once you fill in the Unit test information, the test class location, and create test methods, you will most likely see prompts to Add JUnit 4 library to the build path.

Then, you can run the application with JUnit:

Run -> Run As -> JUnit Test

after selecting the app in Package Explorer.

Your test results will then display with run duration, errors, failures, failure traces, console output, and visual indicators.