JUnit Jupiter allows @Test, @RepeatedTest, @ParameterizedTest, @TestFactory, @TestTemplate, @BeforeEach, and @AfterEach to be declared on interface default methods.
@BeforeAll and @AfterAll can either be declared on static methods in a test interface or on interface default methods if the test interface or test class is annotated with @TestInstance(Lifecycle.PER_CLASS)
@ExtendWith and @Tag can be declared on a test interface so that classes that implement the interface automatically inherit its tags and extensions.
Finally, In your test class, you can implement these test interfaces to have them applied.
Let’s try to understand the above concept using a demo project:
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials</groupId> <artifactId>JUnit5TestInterfacesAndDefaultMethodsExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> <junit.jupiter.version>5.6.2</junit.jupiter.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> </project> |
LifecycleLoggerTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.kkjavatutorials; import java.util.logging.Logger; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.TestInfo; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestInstance.Lifecycle; @TestInstance(value = Lifecycle.PER_CLASS) public interface LifecycleLoggerTest { static final Logger LOG = Logger.getLogger(LifecycleLoggerTest.class.getName()); @BeforeAll default void beforeAll() { LOG.info("This Method runs before all tests"); } @AfterAll default void afterAll() { LOG.info("This Method runs after all tests"); } @BeforeEach default void beforeEach(TestInfo testInfo) { LOG.info(() -> String.format("About to execute [%s]", testInfo.getDisplayName())); } @AfterEach default void afterEach(TestInfo testInfo) { LOG.info(() -> String.format("Finished executing [%s]", testInfo.getDisplayName())); } } |
InterfaceDynamicTestsDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.DynamicTest.dynamicTest; import java.util.stream.Stream; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.TestFactory; public interface InterfaceDynamicTestsDemo { @TestFactory default Stream<DynamicTest> dynamicTestsForPalindromes() { return Stream.of("pop", "radar", "mom", "dad","madam") .map(inputText -> dynamicTest(inputText, () -> assertTrue(isPalindrome(inputText)))); } /** * Method to check whether input String is Palindrome or not * @param inputText * @return return true if input is Palindrome else false */ default boolean isPalindrome(String inputText) { return new StringBuffer(inputText).reverse().toString().equals(inputText); } } |
The output of the above project:
You May Also Like:
Junit 5 Architecture
JUnit 5 Annotations
JUnit 5 Maven Dependency
JUnit 5 with Gradle Dependency
JUnit 5 Test Lifecycle
JUnit 5 @BeforeAll annotation example
Unit 5 @AfterAll annotation example
JUnit 5 @BeforeEach and @AfterEach annotation Example
JUnit 5 Display Names
Assertions in JUnit 5 Examples
Third-party Assertion Libraries support in JUnit 5
JUnit 5 Assumptions Examples
Conditional Test Execution in JUnit 5
JUnit 5 Nested Tests Example
JUnit 5 @Tag Annotation example
Test Execution Order in Junit 5
Dependency Injection and Testing in JUnit 5
That’s all about Test Interfaces and Default Methods in JUnit 5
If you have any feedback or suggestion please feel free to drop in below comment box.