JUNIT JUPITER (Aggregator) Java class library framework analysis
Junit Jupiter (Aggregator) is a framework for writing and executing the Java unit test, which is part of Junit 5.This framework provides a series of annotations and classes to manage test kits and test cases.It is characterized by flexibility, easy expansion and customization, and provides many new features to assist developers to write more neat and read -more readable unit test code.
The core component of Junit Jupiter is Aggregator, which is an annotation interface that can be used to attribute multiple tests or test methods as a group for execution together.Aggregator is marked with @AggregateWith annotations on the test class or test method. By specifying a parameter of the Aggregator type, the specific polymer to be used.
The following is an example that demonstrates how to use the Aggregator of Junit Jupiter to organize the test kit:
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(TestAggregatorProvider.class)
class AggregatorExampleTest {
@TestTemplate
@ExtendWith(TestAggregatorExtension.class)
void aggregateTest(int input1, int input2, int expectedOutput) {
assertEquals(expectedOutput, input1 + input2);
}
static class TestAggregatorProvider implements TestTemplateInvocationContextProvider {
@Override
public boolean supportsTestTemplate(ExtensionContext context) {
return true;
}
@Override
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) {
List<Object[]> testCases = Arrays.asList(
new Object[]{1, 2, 3},
new Object[]{2, 3, 5},
new Object[]{4, 5, 9}
);
return testCases.stream().map(inputs -> TestTemplateInvocationContext.of(inputs, this::getDisplayName));
}
private String getDisplayName(ExtensionContext context, Object[] inputs) {
return String.format("Test with inputs: %d, %d", inputs[0], inputs[1]);
}
}
static class TestAggregatorExtension implements AfterTestExecutionCallback {
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
int input1 = (int) context.getTestMethod().get().getInvocation().get().getArguments().get()[0];
int input2 = (int) context.getTestMethod().get().getInvocation().get().getArguments().get()[1];
int expectedOutput = (int) context.getTestMethod().get().getInvocation().get().getArguments().get()[2];
int actualOutput = (int) context.getExecutionException().get().getWrappedThrowable().get().getMessage();
assertEquals(expectedOutput, actualOutput);
}
}
}
In the above examples, we define a `AggregateTest" method and mark it as `@testTemplate` and @Extendwith (testaggregatorexExion.class)`.The method of `aggregatetest` accepted three parameters, namely` input1`, `input2`, and` extenstedoutput`.This method will add operations to these two inputs and assert whether the result is equal to the expected output.
In order to provide a combination of test data and test methods, we implemented a `TestaggregatorProvider` class and realized the` testTeMPlateInvocontextProvider` interface.In the `PROVIDETESTTEMPLATEMPLATEINVOCONTEXTS` method, we define three test cases and use the` testTeMPlateInvocontext.of` to create the corresponding test context.
We also define a `TestaggregAtorextersion 'and implement the` AfterTERTETEXECUTIONCALLLLLLLK` interface.In the `AFTERTETETEXECUTION method, we obtained the input parameters and expected output of the test method, and the actual output, and an assertion verification.
This is a simple example that shows how to use the Junit Jupiter (Aggregator) to organize and execute the test kit.You can expand and customize this framework according to your needs in order to better meet your test needs.