The technical principles of the Junit Vintage Engine framework and its application in the Java class library
The technical principles of the Junit Vintage Engine framework and its application in the Java class library
Junit Vintage Engine framework is a module in Junit 5. It is mainly used for test cases compatible with Junit 4 and provide interoperability when running with Junit 4.This article will introduce the technical principles of the Junit Vintage Engine framework and explain its application in the Java class library.
Technical principle:
The Junit Vintage Engine framework integrates with other modules of Junit 5 by implementing Junit 5's Testengine interface.It understands the structure of the Junit 4 test case and runs these test cases through the execution engine of Junit 5.This design enables Junit Vintage Engine to run Junit 4 test cases in Junit 5's environment.
In the Junit Vintage ENGINE framework, the Junit 4 test case is encapsulated into the Runner object.Runner object is the core concept of Junit 4, which is used to run test cases and provide test results.The Junit Vintage Engine framework will analyze the Runner object and convert it to the Executable object of Junit 5, and then use the Junit 5 execution engine to run these Executable objects.
At the same time, the Junit Vintage Engine framework also provides Junit 4's rules and compatibility support for listeners.Junit 4's rules and listeners are commonly used expansion mechanisms in Junit 4, which is used to insert custom logic during test operation.The Junit Vintage Engine framework can recognize the rules and monitors of Junit 4 and convert it into a expansion mechanism of Junit 5.In this way, Junit 4's rules and listeners can also work normally in the Junit 5 environment.
Example:
Below is a simple example, demonstrating the application of the Junit Vintage Engine framework in the Java class library:
import org.junit.Test;
import static org.junit.Assert.*;
public class MyTestClass {
@Test
public void testAddition() {
int result = Calculator.add(2, 3);
assertEquals(5, result);
}
// other test methods...
}
public class Calculator {
public static int add(int a, int b) {
return a + b;
}
// other methods...
}
In this example, we have a MyTestClass class that contains a test method TestadDition with @Test 4's @test annotation mark.This test method calls the ADD method of the Calculator class and uses an assertion to verify the results.
In order to run Junit 4 test cases in Junit 5, we need to add Junit Vintage Engine to POM.XML files (if Maven) (if you use Maven):
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.8.0</version>
</dependency>
Then we can use the Junit 5 console to run test cases:
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
public class TestRunner {
public static void main(String[] args) {
LauncherDiscoveryRequestBuilder requestBuilder = LauncherDiscoveryRequestBuilder
.request()
.selectors()
.selectPackage ("com.example.tests"); // Fill in the package name of the test case to be run.
Launcher launcher = LauncherFactory.create();
SummaryGeneratingListener listener = new SummaryGeneratingListener();
launcher.registerTestExecutionListeners(listener);
launcher.execute(requestBuilder.build());
// Output test results Abstract
System.out.println(listener.getSummary());
}
}
In this example, we created a Testrunner class to perform Junit 4 test cases.We use LauncherDisCoveryRequestBuilder to specify the test case package to be run, and use SummarygenRaTingListener to generate the test results abstract.
In this way, we can run Junit 4 test cases in Junit 5 and use the new features and tools of Junit 5 to manage testing.
in conclusion:
The Junit Vintage Engine framework is a compatible layer of Junit 5 and Junit 4. It provides a smooth transition to Junit 5 for developers using Junit 4.It can identify and run Junit 4 test cases and provide compatibility support for the expansion mechanism of Junit 4.By using Junit Vintage Engine, developers can maintain the function of the existing Junit 4 test case while enjoying the new features and tools of Junit 5.
I hope this article will help you understand the technical principles of the Junit Vintage Engine framework and its application in the Java class library.