Tags

, , ,

I was testing one of the news that are about to be released: JUnit 5. The first impression is that it has a lot of cool functionalities, such as parameterized tests, improved Exception assert, grouping tests by Tags and etc.

I created a branch of my java-sample-project project using some JUnit 5 features with Spring Boot and I will summarize just some of the features:

Parameterized Tests

  • It gives you the ability to run the same test with different parameters, coming from different sources: Inline Values (String, Integer, etc.), Enum, CSV, Methods…
  • In my project, I used @MethodSource to exemplify:

  • With this, the test will search for a method called createUsers that returns a Stream<Arguments>:

  • The expected parameters in my test method are: a User and an Integer, and in that Stream I’m passing both. The test will run first with userOne () and 1, and then with userTwo () and 2.

Exceptions Assert

  • With JUnit 4 it was possible to test exceptions, both with @Test(expected=…) and Exception Rule (ExpectedException). This last one could still validate the exception message. But I believe nothing compares to this assertThrows:

  • P.S.: It’s worthy mentioning the use of Lambda Expressions and Streams, as in the case of parameterized tests. JUnit 5 makes a lot of use of the Java 8 features, in fact, JUnit 5 needs Java 8.

Grouped Assertions

  • Rather than having multiple asserts together and if one fails, all fail, it is now possible to group the asserts so that all asserts are executed and all failures will be reported together. For this, assertAll is used:

  • If the first assertion or other(s) fail, then all assertions would still be executed and at the end it would be displayed which failed.

Assert Timeout

  • It gives you the ability to test whether a method will perform within an expected time. For this, the assertTimeout is used:

Display Name of the Test

  • This is pretty cool for a better test report. It allows the display name of the test to be defined through @DisplayName annotation:

  • Ok, I know that my test description was pretty stupid, but you got the point.

Test Repetition

  • It gives you the ability to perform the same test more than once. The annotation @RepeatedTest is used. For better visualization, the annotation has some variables to use in the description, such as displayName, currentRepetition and totalRepetitions.

  • According to the value described in @RepeatedTest, each repetition will show, for example:
    • “Testing exception 1/2”
    • “Testing exception 2/2”

Grouping Tests by Tags

  • That’s pretty cool, too. You can group the tests by tags and run only the tests of a specific tag. To do this, use the annotation @Tag:

  • To run the tests of a specific tag (let’s assume example), just run mvn test -Dgroups=example
  • The @Tag annotation can be used for both classes and methods, and both can have multiple tags defined in the same place

Nested Tests

  • With the @Nested annotation it’s possible to create a test class inside another test class. Really nice for better tests organization.

 Disable Tests

  • Finally, the @Disabled annotation that disables a test/a test class.

Advertisements