Test interfaces
In JUnit 5, there are different rules relative to the use of annotations in Java interfaces. First of all, we need to be aware that @Test
, @TestFactory
, @BeforeEach
, and @AfterEach
can be declared on interface default methods.
Note
Default methods is a feature of Java introduced in version 8. These methods (declared using the reserve keyword default
) allows to define a default implementation for a given method within a Java interface. This capability can be useful for backward compatibility with existing interfaces.
The second rule regarding JUnit 5 and interfaces is that @BeforeAll
and @AfterAll
can be declared on static
methods in a test interface. Moreover, if the test class, which implements a given interface, is annotated with @TestInstance(Lifecycle.PER_CLASS)
, the methods @BeforeAll
and @AfterAll
declared on the interface do not need to be static
, but default
methods.
The third and final rule concerning interfaces in JUnit 5 is @ExtendWith
and @Tag
can be declared on test...