Search⌘ K
AI Features

Test Templates

Explore how to implement custom test templates in JUnit 5 by using the TestTemplate annotation and creating extensions that manage test invocation contexts. Understand how to support multiple test executions with dynamic display names and parameter resolution through practical examples.

We discussed repeated tests and parameterized tests in previous chapters. Methods annotated with @RepeatedTest and @ParameterizedTest aren’t the actual executed tests, but templates for other tests. JUnit 5 has a specific annotation, org.junit.jupiter.api.TestTemplate, that makes a test method a test template.

Creating test templates

To create test templates, we need to create custom extensions that implement the TestTemplateInvocationContextProvider interface. This interface has two methods:

  • The boolean supportsTestTemplate(ExtensionContext context) method checks if
...