JUnit 3.8
1. Add the JUnit Library Under eclipse
Right-click Project> build path> Add library> and select JUnit.
2. Download the jar package from the official website and import it to the project.
3. Write test cases. Create SRC fold under the same level of SRC and name it test. The new test case under test corresponds to the class to be tested. For example. Student. Java -- "studenttest. Java
4. JUnit slogan: Keep the bar green to keep the code clean
5. unit testing is not to prove that you are right, but to prove that you are not wrong.
6. Test Cases are an important aspect of unit testing.
Http://d.download.csdn.net/down/1653850/jerry2011
Http://download.csdn.net/source/1653850#acomment download link
7. The test class must inherit the testcase parent class.
7.1 The Public void does not have a parameter. The method name must start with test.
8. testcase must be completely independent and no dependency is allowed.
9. It cannot depend on the sequence of testcase tests.
10. Dry (don't repeat yourself)
11 Write test first and then instance
12Public static voidMain (string [] ARGs ){//JUnitBuilt-in runner
JUnit. textui. testrunner.Run(Xx.Class);
}
13. What is the status before the test and what is the status after the test.
14. Test the private Method
A. Modify the method access modifier
B. Call the private method of the target class in the test Class Using Reflection. (Recommended)
15. Test Suite testsuite can combine multiple tests to perform multiple tests at the same time.
Create a testall class and add:
import junit.framework.TestCase;import junit.framework.TestSuite;public class TestAll extends TestCase{public static TestSuite suite(){ TestSuite suite = new TestSuite(); suite.addTestSuite(Calculator2Test.class); suite.addTestSuite(DeleteAllTest.class); suite.addTestSuite(CalculateTest.class); return suite; } }
At the same time, how to repeat the test for a method multiple times. Yes
Testsuite suite = new testsuite (); suite. addtestsuite (calculator2test. class); suite. addtestsuite (deletealltest. class); suite. addtestsuite (calculatetest. class); // repeat the test 20 times for the add method in calculate. addtest (New repeatedtest (New calculatetest ("testadd"), 20); Return suite;
Junit4
16 junit4 does not require that the class to be tested must inherit testcase and annotion can be introduced. Add @ test. To the method name.
For example:
public class CalculatorJunit4Test{ private CalculatorJunit4 calculatorJunit4; public CalculatorJunit4Test() { calculatorJunit4 = new CalculatorJunit4(); } @org.junit.Test public void testAdd(){ int result = calculatorJunit4.add(2, 3); Assert.assertEquals(5, result); }}
17 although junit4 does not require the method name format, it must have a good naming convention.
You can use @ before and @ after to implement the setup and teardown methods in junit3.8.
18 Use @ before class and @ afterclass to define a global initialization method. Such as the database link.
@BeforeClass public static void globalInit(){ System.out.println("globalInit()"); } @AfterClass public static void globalDestory(){ System.out.println("globalDestory()"); }
19. Use @ test (timeout = 300) to determine whether a method times out.
Use @ test (expected = Exception. Class) to determine whether an exception has occurred.
Use @ ignore ("string") to declare that a method is not ready yet. Do not perform a test. It can also be used on the entire class.
For example:
@ Test (timeout = 3000) // 3 seconds after timeout, public void testadd () {int result = calculatorjunit4.add (2, 3); assert. assertequals (5, result) ;}@ ignore @ test (expected = Exception. class) // expected to throw an exception public void testdivide () throws exception {calculatorjunit4.divide (1, 0 );}
20 parameterization test (parameters): When a test class uses a parameter to run, the @ parameters annotation must be declared before the class, and the parameter is assigned a value in the constructor. Finally, write the test class.
For example:
import static org.junit.Assert.*;import java.util.Arrays;import java.util.Collection;import java.util.Collections;import org.junit.Assert;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.Parameterized;import org.junit.runners.Parameterized.Parameters;@RunWith(Parameterized.class)public class ParametersTest { private int expected ; private int input1; private int input2; private CalculatorJunit4 cal; @Parameters public static Collection preparedData(){ Object object[][] = {{5,2,3},{8,1,7},{0,-1,1},{20000,10000,10000}}; return Arrays.asList(object); } public ParametersTest(int ex,int inp1,int inp2){ this.expected = ex ; this.input1 = inp1; this.input2 = inp2; } @Before public void setUp(){ System.out.println("setUp"); cal = new CalculatorJunit4(); } @Test public void testAdd(){ assertEquals(this.expected, cal.add(this.input1, this.input2)); }}