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"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.howtodoinjava</groupId>
	<artifactId>JUnit5Examples</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
		<junit.jupiter.version>5.5.2</junit.jupiter.version>
		<junit.platform.version>1.5.2</junit.platform.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<version>${junit.jupiter.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.junit.platform</groupId>
			<artifactId>junit-platform-runner</artifactId>
			<version>${junit.platform.version}</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
			</plugin>
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.22.2</version>
			</plugin>
		</plugins>
	</build>
</project>

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;

@RunWith(JUnitPlatform.class)
public class TestApplication { 
	@Test
	@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.)
    Reply

Leave a Comment

HowToDoInJava

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