Java learns (Junit) from scratch, and java starts from scratch
I. Software Testing
Software Development:
Project Research -- Demand Analysis -- Software Design -- program coding -- software testing -- Operation and Maintenance
Software testing: Use testing tools to test the functions and performance of products according to the testing scheme and process, and use manual or automatic means to run or test a system. The purpose is to test whether the specified requirements are met and identify the differences between the expected results and the actual results.
Cartridge test-white box test-regression test-unit test
Ii. JUnit-unit test tool
Iii. Test the Junit test class
Create a tested class
Package com. pb. junit;/*** 1. create a tested class * 2. junit 3.0 or 4.0 has 3.0*3. create a test class * simple Calculator */public class Calculator {// calculation result private static int result; // add public void add (int n) {result + = n;} // subtract public void minus (int n) {result-= n;} // use the public void mult (int n) Method) {// result * = n;} // Division public void div (int n) {if (n = 0) {System. out. println ("the divisor cannot be 0! ") ;}Else {result/= n ;}// open side: endless loop public void sqr () {(;;) {}}// square public void square (int n) {result = n * n;} // clear the calculation result public void clear () {result = 0 ;} public int getResult () {return result;} public void setResult (int result) {Calculator. result = result ;}}Test with JUnit3
Package com. pb. junit; import junit. framework. assert; import junit. framework. testCase; public class CalculatorTest extends TestCase {// create a test class Object Calculator calculator = new Calculator (); // test the method of running before startup protected void setUp () throws Exception {super. setUp (); calculator. clear (); System. out. println ("before test");} // Method for running after test ends protected void tearDown () throws Exception {super. tearDown (); System. out. println ("after test");} // The test method in JUnit3 must start with "test" public void testAdd () {calculator. add (2); calculator. add (3); // 2 + 3 returns 5 int result = calculator. getResult (); Assert. assertEquals (5, result);} public void testMinus () {calculator. minus (1); calculator. minus (5); // The expected result is-6 int result = calculator. getResult (); Assert. assertEquals (-6, result);} public void testMult () {calculator. mult (3); calculator. mult (4); int result = calculator. getResult (); Assert. assertEquals (12, result);} public void testDiv () {calculator. div (8); calculator. div (0); int result = calculator. getResult (); Assert. assertEquals (4, result );}}Use JUnit4
Package com. pb. junit; 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 CalculatorTest {Calculator calculator = new Calculator (); @ BeforeClass public static void setUpBeforeClass () throws Exception {System. out. println ("============= all tests performed before");} @ AfterClass public static void tearDownAfterClass () throws Exception {System. out. println ("Run Once after all tests ==========") ;}@ Before public void setUp () throws Exception {System. out. println ("=========== run once before each test"); calculator. clear () ;}@ After public void tearDown () throws Exception {System. out. println ("Run Once after each Test ==============");} // @ Test is required, the test method may not start with Test, must be public, and no return must be void @ test public void testAdd () {calculator. add (3); calculator. add (2); int result = calculator. getResult (); assertEquals (5, result);} // Ignore this Test method @ Ignore @ Test public void testMinus () {fail ("Not yet implemented ");} @ Test public void testMult () {fail ("Not yet implemented");} // throw a predefined exception @ Test (expected = ArithmeticException. class) public void testDiv () throws ArithmeticException {calculator. add (8); calculator. div (0); int result = calculator. getResult (); assertEquals (0, result) ;}@ Test (timeout = 5000) // more than 5 seconds, public void testSqr () {calculator. sqr (); fail ("Not yet implemented");} @ Test public void testSquare () {fail ("Not yet implemented ");}}Test Suite
Package com. pb. junit; import org. junit. runner. runWith; import org. junit. runners. suite; import org. junit. runners. suite. suiteClasses; @ RunWith (Suite. class) @ SuiteClasses ({// name of the test case class Calculator1Test. class, Calculator2Test. class, CalculatorTest. class}) public class TotalTest {}