DEV Community

Cover image for Why You Should Start Using JUnit 5

Why You Should Start Using JUnit 5

Jan Wedel on August 05, 2018

This is a cross-posting of an article published on my blog which you can find here. What is JUnit 5? According to its website, JUnit 5 is the ...
Collapse
 
lukefx profile image
Luca Simone

Why they added a @DisplayName and didn't add a param to @test like @test (name = "it should not fails")?

Collapse
 
stealthmusic profile image
Jan Wedel

Ha! That's a very good question that I just recently asked Sam Brannon, one of the core committers of JUnit 5. :)

His answer goes as follows:

  • There are other "things" that support @DisplayName like test class (as shown in my example), @TestFactory, @RepeatedTest, @ParameterizedTest and @TestTemplate. Having @DisplayName orthogonal to those features is just about separating concern.
  • If JUnit would have a concept like @AliasFor meta annotations in spring, Β΄this would still be possible (but it doesn't)
  • You could write your own custom composed annotation
Collapse
 
firuzzz profile image
Jose Luis Romero

An intuitive method/function name is vital in every language, if it's not enough we got documentation and if we got access to source, inner comments too. DisplayName is the excuse to avoid all previous
Nested your example just add more pollution, it is not a good one
PD: I like tests ;)

Collapse
 
stealthmusic profile image
Jan Wedel

Honestly, I don't really understand your point.
Sure, functional readable method names are important. But why would anyone stop doing that in favor of using @DisplayName? Is this just your hypothesis or did you actually see that?

Testing is not about how the code works but what it should do. This you won't get from looking at the code. Tests are the functional specification written in your language of choice. Having @DisplayName just makes this specification more readable.

As I pointed out already, @Nested is more "pollution", however when you execute the tests and you see them properly grouped and named in the IDE, it is very helpful.

Collapse
 
vga profile image
Victor Gallet

SpringExtension is available since Spring 5 not Spring boot 2. This class can be found in spring-test dependency.

Collapse
 
stealthmusic profile image
Jan Wedel

Yep, you are right.

I was looking at this from a spring-boot perspective (maybe a bit narrow-minded) where you don't select the spring framework or testing version anymore. Spring 5 will be used by boot 2 automatically.

Collapse
 
pdrmdrs profile image
Pedro Paulo Paiva de Medeiros

Amazing. I really like the idea of giving names to the tests. The tests that I did always ended up as you said, with bigger names.

Collapse
 
yhippa profile image
Richard Yhip

What does not making methods and classes public buy us? Better readability?

Collapse
 
stealthmusic profile image
Jan Wedel

First, it's not necessary but your IDE will probably warn you (e.g. IntelliJ does it). Second, yes it's just less boilerplate. But I intentionally did not try to communicate that a must-have feature.