Print names of test methods executed by JUnit 5

Use methods names to delimit output of unit tests

Marian C.
5 min readMay 17, 2022

When a piece of code is perfect and its tests always pass, the output of the never failing tests does not matter. It is enough to see in the test report that the tests have passed.

But for development or improvement of fresh code and its tests, the legible output of the tests is essential. It is quite inconvenient that the Maven Surefire plugin does not display the names of the executed test methods. So it is unclear which method produced a particular piece of output. Furthermore, if a test method fails, the error is not displayed until all the methods in a class get executed. If the test class has slow methods, waiting for all the tests in the class to be executed can be a recurrent waste of time.

In this post I describe a simple trick to improve the output of JUnit 5 so the output of each test method is preceded and followed by the name of the method. For that it is not necessary to modify the code of the tests, only an additional dependency needs to be added into the project.

I start with demonstrating the result. A sample test class BTest has two active test methods. Executed first test1() fails because of an exception:

public class BTest {

// sample @BeforeAll…

--

--

Marian C.
Marian C.

Written by Marian C.

Java, JavaScript and SQL developer. Interested in data collection and visualization.