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

Eclipse provides wizards to create JUnit tests. In the editor of the class you want to test, press Ctrl+1 on the class name and select Create new JUnit test case.

Quick Fix to create a JUnit test

Alternatively, right-click the class in the Package Explorer and select New  JUnit Test Case, or use File  New  Other…​  Java  JUnit.

2.2. Running JUnit tests

Right-click a test class and select Run As  JUnit Test to run all its tests. The shortcut is Alt+Shift+X, T. To run a single test, place the cursor on the test method name and use the shortcut.

The JUnit view shows the results. To run a test again, right-click it in this view and select Run.

JUnit view
Eclipse creates a run configuration for each test run. You can change them via Run  Run Configurations…​.

2.3. Comparing results

For a failed equality assertion, select the test and press Compare Actual With Expected Test Result in the tool bar of the Failure Trace. Double-clicking the first line of the failure trace works, too.

Compare Result dialog

2.4. Showing only failed tests

The view menu of the JUnit view lets you show only failed tests.

Show Failures Only

You can also activate the view only if a test fails.

Activate on Error/Failure only

2.5. Copying failed tests and stack traces

Right-click the test results and select Copy Failure List to copy the failed tests and their stack traces to the clipboard.

Copy Failure List

2.6. Showing parameter names with code minings

Code minings add information to the editor without changing the source. For tests, they can show the parameter names of method calls. Enable them under Window  Preferences  Java  Editor  Code Minings.

Code Minings preferences
Parameter names in a test

2.7. Content assist for static imports

Eclipse cannot always add static import statements automatically. To let content assist propose the JUnit methods and add the import, open Window  Preferences  Java  Editor  Content Assist  Favorites. Press New Type…​ and add the following types.

  • org.junit.jupiter.api.Assertions

  • org.junit.jupiter.api.Assumptions

Favorites preferences

Content assist (Ctrl+Space) now proposes methods such as assertEquals and adds the static import.

2.8. Test suites

A test suite runs several test classes together. For JUnit 5, add the org.junit.platform:junit-platform-suite dependency with test scope. Annotate a class with @Suite and select the test classes with @SelectClasses.

import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses({ MyClassTest.class, MySecondClassTest.class })
public class AllTests {
}

2.9. JUnit Plug-in Tests

JUnit Plug-in Tests test Eclipse plug-ins. They run in a separate Eclipse instance which the test runner starts.

2.10. Running tests with Gradle

With the Buildship tooling, right-click a test class or method and select Run As  Gradle Test. The Gradle Executions and Console views show the result.

Run As > Gradle Test
Home Tutorials Training Consulting Books Company Contact us


Get more...