Today, summarize one of the common test methods in Java – Unit test JUnit. The object of the unit test needle is the method in the class, and the method being tested must meet the following conditions:
1. No return value
2. No parameters
3. Public retouching
An example is given below:
import org.junit.Test;publicclass Test2 { @Test publicvoidsum() { int a=9; int b=8; int sum = a+b; }}
The code is very simple, the function is to calculate the @test of two integers, to test the function can be directly in the main function call it, you can also use the unit test method test, the method has a statement, its role is to indicate that this method is a test method, the need to import the corresponding class, Eclipse needs to download and import the Junit.jar package, myeclipse with the appropriate package. When testing, just select the method name, right-click, select the unit test under run as, here is the result of the test:
Unit Test window is issued, there is a green bar, this means that the current test method does not have any problems, if it is dark red then the method test does not pass. We add an output statement to the code:
import org.junit.Test;publicclass Test2 { @Test publicvoidsum() { int a=9; int b=8; int sum = a+b; System.out.println(sum); }}
A second unit test, the results are as follows:
The discovery method passes the test and outputs the calculated results on the console. This is a method to test, you can also test several methods, the following is the code:
Import Org.junit.Test; Public classTest2 {@Test Public void sum() {intA=9;intb=8;intsum = a+b; System. out. println (sum); } @Test Public void sum2() {intA=Ten; System. out. println (2*A); } @Test Public void sum3() {intb=Ten; System. out. println (3*B); }}
Thanks to the three methods, the function is very simple, to unit test these three methods, just click anywhere in the code right mouse button, and then select the unit test under run as, here is the test results:
The result is that all three methods are tested and the results are output at the console, which is much more convenient than one to invoke the test. If now I only want to test the sum and sum3 these two methods, and do not want to sum2 test what to do, very simple answer, just the method on the @test to @ignore on it, no longer described here. In addition to @test and @ignore, there are @after and @before the two statements, their role is to execute the corresponding method before and after the method test, the following is the code:
ImportOrg.junit.After;ImportOrg.junit.Before;ImportOrg.junit.Test; Public class Test2 { @After Public void Testafter() {System.out.println ("after"); }@Test Public void sum() {intA=9;intb=8;intsum = a+b; SYSTEM.OUT.PRINTLN (sum); }@Before Public void Testbefore() {System.out.println ("Before"); }}
Here's the result:
It is found that the Testafter () method is executed before the sum method is executed, and the Testbefore () method is executed after the sum is executed, which is the role of @after and @before.
Unit Test JUnit