What’s up with JUnit 5

JUnit 5 is not yet a GA release but it has started making the buzz around with its all new features, especially the Java8 enthusiasts like me couldn’t wait for the GA release to start using. Architecturally Junit 5 is a whole different animal than its predecessor; I will not talk about the architecture in this article instead will give a glimpse of how this new version of JUnit can improve the tests, if you are interested in understanding the JUnit 5 architecture I suggest a read of JUnit 5 user guide. 

The following are the changes or improvements over JUnit 4

  • There’s no difference in the way of writing basic tests, remains same as the JUnit 4
  • @Before and @After annotations are given new names @BeforeEach and @AfterEach without any change in the work they do
  • In the same lines as above @BeforeClass and @AfterClass are given new names @BeforeAll and @AfterAll; these are also unchanged with respect to the work they do or the constraints the the methods using these annotation need to meet
  • @Ignore got a new as well, @Disabled
  • Categorizing the tests has got a new annotation @Tag in place of @Category

The following are the new features added in Junit 5

  • A couple of years back we had a situation where the business team wanted to have a look at our unit test report with meaningful names. Then we had to create a custom annotation that holds the name of the test cases and used our implementation of RunListener to generate a meaningful report. JUnit 5 now provides the annotation @DisplayName that can be used to provide a name to the test. May sound like a trivial improvement but it really makes it’s case
  • @RepeatedTest annotation that is used to repeat a single test several times is another cool feature that comes handy when number of execution can change the state of an Object or to verify if the repeated execution causes any resource clog
  • Dependency Injection for constructors and the methods, which means the test methods as well as the methods annotated with @BeforeXXX and @AfterXXX can now take the arguments with the default runner. No need to fall back Parameterized class or the TestNG’s DataProviderRunner
  • Parameterized tests are now more flexible; one class can have different data sources, different methods can use different data sources. The test needs to be annotated with @ParameterizedTest and then with a source annotation like @ValueSource. There are different source annotations for different type of sources, will touch upon them in the upcoming articles.
  • Dynamic tests is another exciting feature. The tests are created at run time from a collection or stream of values as below. The dynamic test needs to be annotated with @TestFactory (Test Factory generated the tests at the run time). I will touch upon this in detail with code examples in upcoming articles

Stream dynamicTestsFromStream() {
return Stream.of("your", "input/output", "values").map(
str -> dynamicTest("test" + s, "tr, () -> { /* A lambda of executable; This is where the test implementation goes */ }));

Note: It is to be noted that unlike the regular tests, before and after exeution of the dynamic tests the methods annotated with @BeforeXXX and @AfterXXX are not executed 

  • With JUnit 5, we can write a test class nested inside another test class. Inner class needs to be annotated with @Nested. This is particularly useful to group the tests but don’t want to write whole new outer class.

Hope you enjoyed reading this article, will comeback with the detailed articles about each of these cool features.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s