Test templates

A @TestTemplate method is not a regular test case but a template for test cases. Method annotated like this will be invoked multiple times, depending on the invocation context returned by the registered providers. Thus, test templates are used together with a registered TestTemplateInvocationContextProvider extension.

Let see a simple example of a test template. In the following snippet, we can see a method annotated with @TestTemplate, and also declaring an extension of the type MyTestTemplateInvocationContextProvider:

package io.github.bonigarcia;

import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;

class TemplateTest {

@TestTemplate
@ExtendWith(MyTestTemplateInvocationContextProvider.class)
void testTemplate(String parameter) {
System.out.println(parameter);
}

}

The required provided implements the Jupiter interface TestTemplateInvocationContextProvider. Inspecting the code of this class, we can see how two String parameters are provided to the test template (in this case, the value for these parameters are parameter-1 and parameter-2):

package io.github.bonigarcia;

import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;

public class MyTestTemplateInvocationContextProvider
implements TestTemplateInvocationContextProvider {

@Override
public boolean supportsTestTemplate(ExtensionContext context) {
return true;
}

@Override
public Stream<TestTemplateInvocationContext>
provideTestTemplateInvocationContexts(
ExtensionContext context) {
return Stream.of(invocationContext("parameter-1"),
invocationContext("parameter-2"));
}

private TestTemplateInvocationContext invocationContext(String parameter) {
return new TestTemplateInvocationContext() {
@Override
public String getDisplayName(int invocationIndex) {
return parameter;
}

@Override
public List<Extension> getAdditionalExtensions() {
return Collections.singletonList(new ParameterResolver() {
@Override
public boolean supportsParameter(
ParameterContext parameterContext,
ExtensionContext extensionContext) {
return parameterContext.getParameter().getType()
.equals(String.class);
}

@Override
public Object resolveParameter(
ParameterContext parameterContext,
ExtensionContext extensionContext) {
return parameter;
}
});
}
};
}

}

When the test is executed, each invocation of the test template behaves like a regular @Test. In this example, the test is only writing the parameter in the standard output.

Console output of test template example
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.139.168