This tutorial explains the usage of the Eclipse IDE for creating and executing JUnit test.

1. Introduction to testing

2. Eclipse support for JUnit 5

2.1. Creating JUnit tests

You can write the JUnit tests manually, but Eclipse supports the creation of JUnit tests via wizards.

You can select the class which you want to test in the editor, can press Ctrl+1 and select Create new JUnit test.

create new unit test via quickfix

Alternatively, you can right-click on your class, select this class in the Project Explorer or Package Explorer view, right-click on it and select New  JUnit Test Case. Or, you can also use the JUnit wizards available under File  New  Other…​  Java  JUnit.

2.2. Running JUnit tests

The Eclipse IDE also provides support for executing your tests interactively.

To run a test, select the test class, right-click on it and select Run-as  JUnit Test. This starts JUnit and executes all test methods in this class.

Eclipse provides the Alt+Shift+X, T shortcut to run the test in the selected class.

To run only the selected test, position the cursor on the test method name and use the shortcut.

To see the result of a JUnit test, Eclipse uses the JUnit view which shows the results of the tests. You can also select individual unit tests in this view, right-click on them and select Run to execute them again.

JUnit view

Eclipse creates run configurations for tests. You can see and modify these via the Run  Run Configurations…​ menu.

2.3. Compare results

For some assertions, you can open a dialog to compare the expected and the actual value. Right-click on the test result, and select Compare Result or double-click on the line.

junit compare results

2.4. Show only failed tests

By default, this view shows all tests. You can also configure, that it only shows failing tests.

JUnit view

You can also define that the view is only activated if you have a failing test.

JUnit view

2.5. Extracting the failed tests and stacktraces

To get the list of failed tests, right click on the tests result and select Copy Failure List. This copies the failed tests and the stack traces into the clipboard.

Copy failed tests into clipboard

2.6. Using code mining

The Eclipse IDE supports adding additional information to text editors to enrich the editor content without modifying the original source. This can be especially useful for unit tests, as you can activate the method parameters.

You can activate that via Windows  Preference  Java  Editor  Code Minings.

code mining unittests10
code mining unittests20

2.7. Setting Eclipse up for using JUnits static imports

The Eclipse IDE cannot always create the corresponding static import statements automatically.

You can configure the Eclipse IDE to use code completion to insert typical JUnit method calls and to add the static import automatically. For this open the Preferences via Window  Preferences and select Java  Editor  Content Assist  Favorites.

Use the New Type button to add the following entries to it:

  • org.junit.Assert

  • org.hamcrest.CoreMatchers

  • org.hamcrest.Matchers

This makes, for example, the assertTrue, assertFalse and assertEquals methods directly available in the Content Assists.

Adding static imports to the preferences

You can now use Content Assists (shortcut: Ctrl+Space) to add the method and the import.

2.8. Wizard for creating test suites

You can create a test suite via Eclipse. For this, select the test classes which should be included in suite in the Package Explorer view, right-click on them and select New  Other…​  JUnit  JUnit Test Suite.

Create a test suite

Unfortunately JUnit 5 does not yet support test suites, hence the creation is limited to old JUnit 3 or JUnit 4 test suites.

2.9. JUnit Plug-in Test

JUnit Plug-in tests are used to write unit tests for your plug-ins. These tests are executed by a special test runner that launches another Eclipse instance in a separate VM. The test methods are executed within that instance.

2.10. Using Gradle

If you are using Eclipse it is best to install the Buildship tooling. Then you can start your tests via Run as  Gradle Test. The result of the test execution will be displayed in the Console view.

Menu entry "Gradle Test"" loading="lazy">