Annotation is a token for a property, method, or class, such as a @override representation of a method in a parent class.
"1" @Test: Test method (indicates that the method is a test method)
a) (Expected=xxexception.class)
b) (timeout=xxx)
@Test (expected=java.lang.arithmeticexception.class,timeout=100)
//Expect to throw an arithmetic exception, and the runtime is limited to 100ms (usually set when the test code is running efficiently)Public void Testdivide () {
int z = new T (). Divide (8,0);
//assertthat (Z,is (4));
}
"2" @Ignore: Ignored test methods
sometimes some methods do not have the test conditions, temporarily can not test or some of the methods do not need to do the test, this will be able to ignore the operation.
Sometimes the test condition of the method is not satisfied, the whole project is still one module, you can use this method to assume that the test condition is established.
"3" @Before: Run before each test method (@Test method)
[email protected]: Run after each test method
Application:
some of the methods that need to be executed require some prerequisites, such as opening a file, closing the file after execution, and requiring before and after operations.
[email protected]: Run before all tests start
[email protected]: Run after all tests are finished
@BeforeClass
Public static void Beforeclass () {
System.out.println ("Beforeclass");
}
@AfterClass
Public static void Afterclass () {
System.out.println ("Afterclass");
}
if the method is not declared as static, an error will be made after the operation, as follows:
Java.lang.Exception:Method Beforeclass () should be static
Note: The method here must be static, because it is called when the method is invoked before all classes are initialized, and therefore can only be static.
Application:
use Beforeclass when we need to make some time-consuming resources before testing, or when we need to carry some time-consuming configuration environment (establishing a connection with the database, building a database connection pool, and building a spring environment). If you need to release the resources or unload the environment, use Afterclass.
Beforeclass and before are different, one is the global, one is local, such as the database link, it must be built before the global operation, can not execute each method is built once, so the efficiency is too low.
Junit--annotation