The previous section briefly introduced what is unit test, why unit test, and the preliminary use of junit4. Here we will talk about annotations in junit4.
================ Knowledge point of this section ================================= ====
* Error and failures
*Junit4 Annotation
========================================================== ======================================
1. Before commenting, let's first understand the differences between error and failures.
Errors:IndicatesProgramError
@ Test
Public Void Testadd (){
Int Z = New T (). Add ( 5 , 3 );
Assertequals ( 8 , Z );
Int A = 8 / 0 ; // This sentence is incorrect.
}
The running method has the following error prompt:
Failures: indicates that the test fails.
@ Test
PublicVoidTestadd (){
IntZ=NewT (). Add (5,4); // Here, the value is modified. It is correct to set 4 to 3.
Assertequals (8, Z );
}
To run this method, check the error message:
Therefore, when writing a test program, we must first ensure that errors is correct, and then check whether failures has any errors.
2. The following describes common annotations of junit4.
Bytes -----------------------------------------------------------------------------------------------
* @ Test:Test Method
A)(Expected = xxeception. Class)
B)(Timeout = xxx)
*. @ Ignore:Ignored Test Method
*. @ Before:Cloud line before each test method.
*. @ After:Run each test method.
*. @ BefreclassRun all tests before they start.
*. @ AfterclassRun all the test results.
Bytes ------------------------------------------------------------------------------------------------
The following uses a test program to explain the meaning of these annotations:
Package Com. junit4.cc. test;
Import Static Org. JUnit. assert. * ;
Import Static Org. hamcrest. matcher. * ;
Import Org. JUnit. test;
Import Com. junit4.cc. * ;
Import Org. JUnit. before;
Import Org. JUnit. beforeclass;
Import Org. JUnit. afterclass;
Import Org. JUnit. After;
Import Org. JUnit. Ignore;
Public Class Ttest {
@ Beforeclass // Before running.
Public Static Void Beforeclass (){
System. Out. println ( " ------------ Beforeclass " );
}
@ Afterclass // Run after all methods are run
Public Static Void Afterclass (){
System. Out. println ( " ------------- Afterclass " );
}
@ Before // Run each test method before running
Public Void Before (){
System. Out. println ( " ======= Before " );
}
@ After // Run each test method
Public Void After (){
System. Out. println ( " ======= After " );
}
@ Test
Public Void Testadd (){
Int Z = New T (). Add ( 5 , 3 );
Assertequals ( 8 , Z );
System. Out. println ( " Test Run Through " );
}
@ Test ()
Public Void Testdivision (){
System. Out. println ( " In test division " );
}
@ Ignore // Indicates that this method is not run.
@ Test
(Expected = Java. Lang. arithmeticexception. Class , Timeout = 100 ) // Timeout indicates that the operation must be completed within 100 milliseconds. Otherwise, an error is returned.
Public Void Testdivide (){
Int Z = New T (). Divide ( 8 , 2 );
}
}
The running result is as follows:
Mark the Red Star (*) The method runs once at the beginning and end of each method.
Mark the Green Star (*.
JUnit has a variety of annotations, which we usually use.