O'Reilly logo

Mastering Software Testing with JUnit 5 by Boni García

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

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 Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required