This tutorial explains the usage of the Eclipse IDE for creating and executing JUnit test.
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.

Alternatively, right-click the class in the Package Explorer and select , or use .
2.2. Running JUnit tests
Right-click a test class and select 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.

| Eclipse creates a run configuration for each test run. You can change them via . |
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.

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

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

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.

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 .


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 .
Press New Type… and add the following types.
-
org.junit.jupiter.api.Assertions -
org.junit.jupiter.api.Assumptions

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 . The Gradle Executions and Console views show the result.

3. JUnit Resources
3.1. vogella Java example code
If you need more assistance we offer Online Training and Onsite training as well as consulting