OpenTest4j frame
OpenTest4J framework is a powerful tool for Java testing. It provides a wealth of assertions to test the functions and correctness of the Java library.In this article, we will introduce some of the commonly used assertions provided by the OpenTest4j framework and explain their use through the Java code example.
The assertion method of OpenTest4J framework is mainly divided into the following categories:
1. Equivalence:
The OpenTest4J framework provides a wealth of equivalent words to check whether the two objects are equal.These include assertion that the two objects are equal and the two objects are not equal.Here are some examples:
import org.junit.jupiter.api.Assertions;
public class EqualityAssertionsExample {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
String str3 = "World";
Assertions.asserTequals (STR1, STR2); // Equipped STR1 and STR2 equal
Assertions.assertnotequals (STR1, STR3); // Eclabo STR1 and STR3 are not equal
}
}
2. Real assertion:
The OpenTest4J framework provides multiple authentic assertions to determine whether the given conditions are true.This includes asserting Boolean values as true, assertive Boolean values as false.Here are some examples:
import org.junit.jupiter.api.Assertions;
public class TruthAssertionsExample {
public static void main(String[] args) {
int num1 = 5;
int num2 = 10;
Assertions.assertrue (num1 <num2); // Ecclail Num1 less than NUM2
Assertions.assertfalse (num1> num2); // asserted that NUM1 greater than num2 is false
}
}
3. Abnormal assertion:
The OpenTest4J framework provides multiple methods to assert the abnormalities to verify whether the code is thrown out of the expected abnormalities.The following is an example:
import org.junit.jupiter.api.Assertions;
public class ExceptionAssertionsExample {
public static void main(String[] args) {
Assertions.assertThrows(ArithmeticException.class, () -> {
int num1 = 10;
int num2 = 0;
int result = num1 / num2;
});
}
}
In the above code, the `AssertThrows` method asserts that the removal operation will throw the Arithmeticexception abnormalities. If the exception is not thrown, the test will fail.
Summarize:
OpenTest4J framework assertion provides powerful tools for the test of the Java class library, which can help developers write comprehensive and reliable test cases.By using these assertions, developers can ensure the functions and correctness of the Java library, and improve the quality and maintenance of code.
references:
-Opentest4j official document: https://opeentest4j.github.io/