In. NET learning, when exposed to NUnit, to Java Learning, of course, the study of JUnit, this blog brief introduction of JUnit's use.
1. Right-click-->build path-->add Libraries in the project root directory
Figure 1 Adding a jar package
2. Select JUnit under the Add Library and NEXT-->JUNIT4.
After selecting Finish, you will find that there is one more class library under the project:
3. Tested class
Test object public class Tool {//test method one public int add (int a,int b) {return a+b;} Test method two public int add (int a,int b,int c) {return a+b+c;}}
4. New Test class
Import Org.junit.after;import Org.junit.before;import Org.junit.test;public class Tooltest {/* * use junit Steps * 1. Set up test class Tool Test ==> Specification ==> class name + Test suffix * 2. Guide Package * 3. Create a running test method * 1> creates a method of Publi void, with the method name beginning with test, ending with the method name to be tested (canonical) 2> Enter Note @test */@Testpublic void TestAdd1 () {Tool tool = new tool (); int result = Tool.add (1, 2);//3system.out.println ( result);} @Testpublic void TestAdd2 () {Tool tool = new tool ();//int i = 1/0;int result = Tool.add (1, 2,3);//6system.out.println (R Esult);} @Beforepublic void init () {System.out.println ("Call me before each test method runs, initialize some parameters!");} @Afterpublic void Destory () {System.out.println ("Call me at the end of each test method run, release some resources!");}}
5. Select the test method and right-click-->run as-->junit test to run the test
6. The result of the operation is as follows, indicating that the test
Summarize:
The use of JUnit and NUnit is very similar, when we usually do the demo, if we use the main method of the console program, can only execute one thread at a time, if you want to imitate the client to invoke multiple methods can be very troublesome, junit for us to solve the problem well With JUnit, however, testing how many methods, as long as one step, it is very convenient.
Unit Testing with JUnit