JUNIT JUPITER (aggregator) in the Java Library Guide Guide
JUNIT JUPITER (aggregator) in Java Library Guide Guide
Junit Jupiter is a Java class library for writing and running unit tests.It is part of Junit 5, which provides some new functions and enhancements to make the unit test more flexible and powerful.One of them is Junit Jupiter Aggregator, which allows multiple test classes to execute.
In this article, we will introduce the use guidelines of Junit Jilgregator and provide some Java code examples to demonstrate its functions.
1. Add dependencies
First, you need to add Junit Jupiter Aggregator to the dependencies of the project.In the Maven project, the following dependencies can be added to the POM.XML file:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
2. Create a test class
Next, create a test class and add some test methods to it.For example, suppose we have two tests: `CalculatorTest` and` StringmanipulatorTest`, which contain some test methods.
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class CalculatorTest {
@Test
public void testAddition() {
Calculator calculator = new Calculator();
int result = calculator.add(2, 3);
Assertions.assertEquals(5, result);
}
// Other test methods ...
}
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class StringManipulatorTest {
@Test
public void testReverseString() {
StringManipulator stringManipulator = new StringManipulator();
String result = stringManipulator.reverse("JUnit Jupiter");
Assertions.assertEquals("retipuJ niTUJ", result);
}
// Other test methods ...
}
3. Create a test aggregate
Next, create a test polymerization class and use the@@includetags `annotation to specify the test class to be included.
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Tags;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.Extensions;
import org.junit.jupiter.api.TagExtension;
@Extensions({
@ExtendWith(TagExtension.class)
})
@Tags({
@Tag("calculator"),
@Tag("stringManipulator")
})
public class AllTests {
}
In the above examples, we use the@tags` annotation to specify the test classes that are included in the polymer (`CalculatorTest` and` StringManipulatorTest`).The annotation of `@Extendwith (TageXTENSION.Class)` @Extendwith (TageXtent.class) is the key part of Junit Jupiter Aggregator.
4. Run the test polymerization
Finally, we can run the test polymerization class to perform all the tests included in it.
Use the junit operator or execute the following code:
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
public class TestRunner {
public static void main(String[] args) {
LauncherDiscoveryRequestBuilder requestBuilder = LauncherDiscoveryRequestBuilder.request();
requestBuilder.selectors(RequestSelectors.selectClass(AllTests.class));
Launcher launcher = LauncherFactory.create();
SummaryGeneratingListener listener = new SummaryGeneratingListener();
launcher.registerTestExecutionListeners(listener);
launcher.execute(requestBuilder.build());
TestExecutionSummary summary = listener.getSummary();
// You can use Summary here to get test results and statistics
}
}
The above sample code creates a test operator, selects the execution of the `AllTests` aggregate class, and register the abstract of the test results.
So far, you have successfully completed the use of Junit Jupiter Aggregator.By aggregating multiple test classes, you can manage and perform testing easier, and you can also get a overall test report and statistical information.
I hope this article will help you understand the guidelines of Junit Jupiter Aggregator.I wish you a high -quality unit test!