Java Magazine, Nov/Dec 2016
ORACLE COM JAVAMAGAZINE NOVEMBER DECEMBER 2016 15 junit 5 tations can be declared individually or they can be composed to create custom annotations In the following section I describe each annotation and give details with examples @ DisplayName Its now possible to display a name for a test class or its methods by using the @ DisplayName annotation As shown in Listing 2 the description can contain spaces and special characters It can even contain emojis such as J Listing 2 @ DisplayName This is my awesome test class class SimpleNamedTest @ DisplayName This is my lonely test method @ Test void simpleTestIsPassing assertTrue true @ Disabled The @ Disabled annotation is analogous to the @ Ignore annotation of JUnit 4 and it can be used to disable the whole test class or one of its methods from execution The reason for disabling the test can be added as a description to the annotation as shown in Listing 3 Listing 3 class DisabledTest @ Test @ Disabled test is skipped void skippedTest fail feature not implemented yet @ Tags and @ Tag Its possible to tag test classes their methods or both Tagging provides a way of filtering tests for execution This approach is analogous to JUnit 4 s Categories Listing 4 shows a sample test class that uses tags Listing 4 @ Tag marvelous test @ Tags@ Tag fantastic test @ Tag awesome test class TagTest @ Test void normalTest @ Test @ Tag fast test void fastTest You can filter tests for execution or exclusion by providing tag names to the test runners The way to run ConsoleLauncher is described in detail in Part 2 of this article With ConsoleLauncher you can use the t parameter for providing required tag names or the T parameter for excluding tag names @ BeforeAll @ BeforeEach @ AfterEach and @ AfterAll The behavior of these annotations is exactly the same as the behavior of JUnit 4 s @ BeforeClass @ Before @ After and @ AfterClass respectively The method annotated with If an assumption fails it does not mean the code is broken but only that the test provides no useful information The default JUnit runner ignores such failing tests This approach enables other tests in the series to be executed
You must have JavaScript enabled to view digital editions.