Test Interfaces and Default Methods
On interface default methods, JUnit Jupiter allows you to specify @Test, @RepeatedTest, @ParameterizedTest, @TestFactory, @TestTemplate, @BeforeEach, and @AfterEach.
If the test interface or test class is annotated with @TestInstance(Lifecycle.PER CLASS), @BeforeAll and @AfterAll can be declared on static methods in a test interface or on interface default methods.
On a test interface, @ExtendWith and @Tag can be defined so that classes that implement the interface inherit the tags and extensions automatically.
Finally, you can implement these test interfaces in your test class to have them applied. Let us create a demo example to test an interface and default method using JUnit.
Program
@TestInstance(Lifecycle.PER_CLASS)
interface TestLifecycleLogger {
static final Logger logger = Logger.getLogger(TestLifecycleLogger.class.getName());
@BeforeAll
default void beforeAllTests() {
logger.info("Before all tests");
}
@AfterAll
default void afterAllTests() {
logger.info("After all tests");
}
@BeforeEach
default void beforeEachTest(TestInfo testInfo) {
logger.info(() -> String.format("About to execute [%s]",
testInfo.getDisplayName()));
}
@AfterEach
default void afterEachTest(TestInfo testInfo) {
logger.info(() -> String.format("Finished executing [%s]",
testInfo.getDisplayName()));
}
}
interface TestInterfaceDynamicTestsDemo {
@TestFactory
default Stream<DynamicTest> dynamicTestsForPalindromes() {
return Stream.of("racecar", "radar", "mom", "dad")
.map(text -> dynamicTest(text, () -> assertTrue(isPalindrome(text))));
}
}

You can also try this code with Online Java Compiler
Run Code
You can implement these test interfaces in your test class to have them used.
Program
class TestInterfaceDemo implements TestLifecycleLogger,
TimeExecutionLogger, TestInterfaceDynamicTestsDemo {
@Test
void isEqualValue() {
assertEquals(1, "a".length(), "is always equal");
}
}

You can also try this code with Online Java Compiler
Run Code
Output
INFO example.TestLifecycleLogger - Before all tests
INFO example.TestLifecycleLogger - About to execute [dynamicTestsForPalindromes()]
INFO example.TimingExtension - Method [dynamicTestsForPalindromes] took 19 ms.
INFO example.TestLifecycleLogger - Finished executing [dynamicTestsForPalindromes()]
INFO example.TestLifecycleLogger - About to execute [isEqualValue()]
INFO example.TimingExtension - Method [isEqualValue] took 1 ms.
INFO example.TestLifecycleLogger - Finished executing [isEqualValue()]
INFO example.TestLifecycleLogger - After all tests
FAQs
-
Are interface methods public by default?
You can skip the public modifier since all abstract, default, and static methods in an interface are automatically public. Constant declarations can also be found in interfaces. In an interface, all constant values are implicitly public, static, and final.
-
How is the interface used in unit testing?
You can use an abstract test case to test an interface with common tests independent of implementation, and then generate concrete instances of the test case for each implementation of the interface.
-
Can we apply @BeforeAll and @Afterall annotations before all methods in a test interface?
If the test interface or test class is annotated with @TestInstance(Lifecycle.PER CLASS), @BeforeAll and @AfterAll can be declared on static methods in a test interface or on interface default methods.
Check out JUnit Interview Questions here.
Key Takeaways
In this blog we discussed thoroughly about testing interfaces and default methods in JUnit. Testing the logic of default methods in interfaces can be of utmost importance in big projects. At the same time, creating implementations of interfaces and then testing them is cumbersome and time consuming. Thus, we discussed a solution from JUnit to avoid such situations. We then discussed an example revolving around the same solution. We saw different annotations and the methods before which they can be applied.
Yet there is a lot more to learn. Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!