1. Overview
2. Software Testing
3. The JUnit framework
4. Assert
5, TestCase class
6. Create the Tested class
package com.ljb.junit;/** * tested Class (import Junit.jar) * @author ljb * @ version 2015 March 13 */public class Calculator { // Results private static int result; // public void add (int n ) { result += n; } // minus public void minus (int n) { result -= n; } // by public void mult (int n) { result *= n; } // except public void div (int n) { if (n == 0) { SYSTEM.OUT.PRINTLN ("Divisor cannot be 0!"); } else { result /= n; } // zeroing public void clear () { result = 0; } // Obtain the result of the Operation &NBsp;public int getresult () { return result; }}
7, creating test classes
package com.ljb.junit;import junit.framework.assert;import junit.framework.testcase;/** * Create tested Classes * @author LJB * @version 2015 March 13 */public class calculatortest extends testcase { // creating a tested class object Calculator calc = New calculator (); protected void setup () throws Exception { Super.setup (); system.out.println ("Pre-Test ..."); calc.clear (); } protected Void teardown () throws exception { super.teardown (); System.out.println ("After test ..."); } public void testadd () { calc.add (3); calc.add (2); int result = calc.getresult (); assert.assertequals (5, result); } public void testminus () { calc.minus (1); Calc.minus (2); int resulT = calc.getresult (); assert.assertequals ( -3, result); } public void testmult () { calc.add (1); calc.mult (2); int result = calc.getresult (); assert.assertequals (2, result); } public void Testdiv () { calc.add (5); calc.div (2); int result = Calc.getresult (); system.out.println (result); assert.assertequals (2, result); }}
Note: Removing an assert is also possible because TestCase inherits the Assert class
Operation Result:
Before the test ...
After the test ...
Before the test ...
After the test ...
Before the test ...
After the test ...
Before the test ...
2
After the test ...
8, Junit4 commonly used annotations
9. Abnormal test
10. Time Test
Software testing (Junit3 JUNIT4)