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:
- 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.
- 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: