JUnit 5 – Execute Tests in Eclipse

Learn to execute junit 5 tests in eclipse IDE. Maven has been used to import dependencies in this junit 5 example.

1. Junit 5 Maven Dependencies

To be able to execute junit 5 tests in eclipse, we will need following dependencies.

  • junit-platform-runner in test scope: location of the JUnitPlatform runner
  • junit-jupiter-api in test scope: API for writing tests, including @Test, etc. It is transitively included when we include junit-jupiter-engine.
  • junit-jupiter-engine in test runtime scope: implementation of the Engine API for JUnit Jupiter.
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

2. @RunWith(JUnitPlatform.class)

JUnitPlatform allows the test to be run with IDEs and build systems that support JUnit 4 but do not yet support the JUnit Platform directly.

package net.restfulapi.demo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

public class TestApplication { 
	@DisplayName("My First Test")
	void myFirstTest(TestInfo testInfo) {
		NumericCalculator calculator = new NumericCalculator();
		Assertions.assertEquals(2, calculator.add(1, 1), "1 + 1 = 2");
		Assertions.assertEquals("My First Test", testInfo.getDisplayName(), 
									() -> "TestInfo is injected correctly");

Where NumericCalculator class is:

package net.restfulapi.demo;

public class NumericCalculator {
	public int add(int a, int b) {
		return a + b;

3. Demo

Now run the rest as junit testcase in eclipse.

You will get below output.

Junit 5 eclipse example
Junit 5 eclipse example

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

1 thought on “JUnit 5 – Execute Tests in Eclipse”

  1. How to run the Same suite using maven command.

    mvn -Dtest=>testsuiteName> test

    I got below error:

    Failed to execute goal 
    org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test) 
    on project BmsAPITest: No tests were executed!  
    (Set -DfailIfNoTests=false to ignore this error.)

Leave a Comment


A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.