Java starts from scratch 46 (Junit)

Source: Internet
Author: User
Tags assert

First, software testing

Software development:

Project research--requirement analysis--Software Design--Program code--Software testing--operation and maintenance

Software testing: The process of using test tools to perform functional and performance testing of the product in accordance with test scenarios and processes, or to run or test a system by manual or automated means. The aim is to check whether the required requirements are met and to identify the difference between the expected and actual results.

Cartridge test-white box test-regression test-unit test

Second, junit-Unit test Tool

Third, test JUnit test class

To create the class being tested
 PackageCom.pb.junit;/**create tested Class *2.junit 3.0 or 4.0 there are 3.0. Create a test class * simple implementation plus, minus, good, except the calculator*/ Public classCalculator {//result of Operation    Private Static intresult; //addition     Public voidAddintN) {Result+=N; }    //Subtraction         Public voidMinus (intN) {Result-=N; }        //Good Way         Public voidMultintN) {            //result*=n;        }        //Division         Public voidDivintN) {            if(n==0) {System.out.println ("The divisor cannot be 0!"); }Else{result/=N; }        }        //Radical : Dead Cycle         Public voidSqr () { for(;;) {                            }        }                //Square         Public voidSquareintN) {Result=n*N; }        //Clear 0 Operation results         Public voidClear () {result=0; }                         Public  intGetResult () {returnresult; }         Public  voidSetresult (intresult) {Calculator.result=result; }        }
Using the JUNIT3 test
 PackageCom.pb.junit;ImportJunit.framework.Assert;Importjunit.framework.TestCase; Public classCalculatortestextendsTestCase {//To create a test class objectCalculator calculator=NewCalculator (); //methods to run before the test starts    protected voidSetUp ()throwsException {Super. SetUp ();        Calculator.clear (); System.out.println ("Before testing"); }    //methods to run after the test is finished    protected voidTearDown ()throwsException {Super. TearDown (); System.out.println ("After testing"); }    //the test method in JUNIT3 must begin with testing     Public voidTestadd () {Calculator.add (2); Calculator.add (3);//2+3 result is 5                intresult=Calculator.getresult (); Assert.assertequals (5, result); }     Public voidTestminus () {Calculator.minus (1); Calculator.minus (5);//expected result is-6        intresult=Calculator.getresult (); Assert.assertequals (-6, result); }     Public voidTestmult () {Calculator.mult (3); Calculator.mult (4); intresult=Calculator.getresult (); Assert.assertequals (12, result); }     Public voidTestdiv () {Calculator.div (8); Calculator.div (0); intresult=Calculator.getresult (); Assert.assertequals (4, result); }    }
Using JUNIT4

 PackageCom.pb.junit;Import Staticorg.junit.assert.*;ImportOrg.junit.After;ImportOrg.junit.AfterClass;ImportOrg.junit.Before;ImportOrg.junit.BeforeClass;ImportOrg.junit.Ignore;Importorg.junit.Test; Public classcalculatortest {Calculator Calculator=NewCalculator (); @BeforeClass Public Static voidSetupbeforeclass ()throwsException {System.out.println ("============ before all tests are performed"); } @AfterClass Public Static voidTeardownafterclass ()throwsException {System.out.println ("Execute ========== once after all tests"); } @Before Public voidSetUp ()throwsException {System.out.println ("========== each test before executing");    Calculator.clear (); } @After Public voidTearDown ()throwsException {System.out.println ("Execute ============= once per test"); }    //@Test is required, the test method may not start with test, must be public, no return must be void@Test Public voidTestadd () {Calculator.add (3); Calculator.add (2); intresult =Calculator.getresult (); Assertequals (5, result); }    //Ignore this test method@Ignore @Test Public voidTestminus () {Fail ("Not yet implemented"); } @Test Public voidTestmult () {Fail ("Not yet implemented"); }    //throws a pre-defined exception@Test (expected=arithmeticexception.class)     Public voidTestdiv ()throwsarithmeticexception {calculator.add (8); Calculator.div (0); intresult=Calculator.getresult (); Assertequals (0, result); } @Test (Timeout=5000)//more than 5 seconds, the test failed     Public voidTestsqr () {CALCULATOR.SQR (); Fail ("Not yet implemented"); } @Test Public voidTestsquare () {Fail ("Not yet implemented"); }}
Test suite

 PackageCom.pb.junit;ImportOrg.junit.runner.RunWith;ImportOrg.junit.runners.Suite;Importorg.junit.runners.Suite.SuiteClasses; @RunWith (Suite.class) @SuiteClasses ({//name of the test case classCalculator1test.class, Calculator2test.class, Calculatortest.class    }) Public classtotaltest {}

Java starts from scratch 46 (Junit)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.