JUnit4 tutorial-Quick Start and junit4 tutorial
Preface
When I first learned java, I heard about JUnit, the unit testing framework, and a very useful testing framework. The JUnit testing framework makes testing easier and easier. Writing Test code is also simple and clear, powerful functions. Today, I will share with you the usage of JUnit4, just a few minutes!
Import jar package
Import the appropriate jar packages in the integrated development environment: junit-4.12.jar, hamcrest-core-1.3.rc2.jar, hamcrest-library-1.3.rc2.jar. If you only import the first jar package, you will not be able to start the test program. For details about the error and the relationship between the several packages, see my blog: JUnit4 unit test. Here, we provide you with the Free Download Links for these jar packages:
Download junit-4.12.jar
Download hamcrest-library-1.3.rc2.jar
Download hamcrest-core-1.3.rc2.jar
The import method is as follows:
Create test case
Right-click the package name, click New, or choose JUnit test case from the newly created others file, as shown in:
Next, name the test class and select the class to be tested, as shown in:
Click Next and select the test method in the class to be tested, as shown in:
The creation is completed as follows (the first method is the generation method, and the last few are manually written by myself ):
Package JUnitTest; import static org. junit. assert. *; import org. junit. after; import org. junit. afterClass; import org. junit. before; import org. junit. beforeClass; import org. junit. ignore; import org. junit. test; public class TestJava {@ Testpublic void testMain () {fail ("Not yet implemented") ;}@ Testpublic void testTest () {System. out. println ("@ Test"); // call the method you want to Test} @ Testpublic void testAssert () {assertEquals ("chenleixing", "chenlei ");} @ Test (timeout = 1) public void testTimeout () {System. out. println ("timeout test") ;}@ Beforepublic void testBefore () {System. out. println ("@ Before") ;}@ BeforeClasspublic static void testBeforeClass () {// the static method System must be used. out. println ("@ BeforeClass");} @ Afterpublic void testAfter () {System. out. println ("@ After") ;}@ AfterClasspublic static void testAfterClass () {// The static method System must be used. out. println ("@ AfterClass");} @ Ignorepublic void testIgnore () {System. out. println ("@ Ignore ");}}
JUnit4 Annotation
1. @ Test: Test method. The method that the Test program runs. The following parameters can represent different tests, for example (expected = XXException. class) exception test, (timeout = xxx) timeout Test
2. @ Ignore: ignored Test Method
3. @ Before: Run Before each test method
4. @ After: Run After each test method.
5. @ BeforeClass: Run before all tests start
6. @ AfterClass: Run after all tests are completed.
The fail method indicates that the test fails.
AssertEquals test whether two parameters are equal. For details, refer to the corresponding API
For more methods, refer to the relevant API documentation (share the download link at the end of the blog)
Run Test Cases
Write your Test code in the @ Test annotation method and click Run New Test case. The result is as follows:
The difference indicates that the test fails. testMain () and testAssert () will certainly fail, and the test is successful.
The console outputs the following results:
<Span style = "font-size: 18px; "> @ BeforeClass @ Before timeout Test @ After @ Before @ After @ AfterClass </span>
Download jar packages and documents for free
Download junit-4.12.jar
Download hamcrest-library-1.3.rc2.jar
Download hamcrest-core-1.3.rc2.jar
Download junit4API.chm.html
For more details about JUnit4 advanced, refer to: JUnit4 advanced-from simple to deep
Reproduced please note-Author: Java my life (Chen Lei xing) Original Source: http://blog.csdn.net/chenleixing/article/details/44259453