Java Unit Test

Source: Internet
Author: User

1  PackageCom.bxw.demo;2 3  Public classCalculator {4      Private Static intResult//for storing run results5          Public voidAddintN) {6result = result +N;7         }8          Public voidSubstract (intN) {9result = Result-1;//result =result-nTen         } One          Public voidMultiplyintN) { A}//This method has not been completed -          Public voidDivideintN) { -result = result/N; the         } -          Public voidSquareintN) { -result = n *N; -         } +          Public voidSquareRoot (intN) { -              for(; ;) ;//dead Loop +         } A          Public voidClear () {//clear the results 0 atresult = 0; -         } -          Public intGetResult () { -             returnresult; -         } -}
Demo

The above code is used to do the Java unit Test.

1  Packagecom.bxw.test;2 3 Import Staticorg.junit.assert.*;4 5 ImportOrg.junit.Before;6 ImportOrg.junit.Ignore;7 Importorg.junit.Test;8 9 ImportCom.bxw.demo.Calculator;Ten  One /** A * Unit Test Demo - * You need to add a @test callout to the method you want to test, indicating that this is a test method.  - * [email protected] indicates that this is an incomplete method, skipping the test the * [email protected] code that must be executed before any test execution - * [email protected] code that must be executed after any test execution - * [email protected] (timeout=1000) Limited time Test -  * @authorAdministrator +  * -  */ +  Public classCalculatortest { A     //create a class to test at      Private StaticCalculator Calculator =NewCalculator (); -          - @Before -          Public voidSetUp ()throwsException { - calculator.clear (); -         } in         //Anomaly Testing -@Test (expected = ArithmeticException.class ) to          Public voidDivideByZero () { +Calculator.divide (0);  -        }  the  * @Test $          Public voidTestadd () {Panax NotoginsengCalculator.add (2); -Calculator.add (3); theAssertequals (5, Calculator.getresult ()); +         } A  the @Test +          Public voidtestsubstract () { -Calculator.add (10); $Calculator.substract (2); $Assertequals (8, Calculator.getresult ()); -         } -  the@Ignore ("Multiply () not yet implemented") - @TestWuyi          Public voidtestmultiply () { the         } -  Wu @Test -          Public voidtestdivide () { AboutCalculator.add (8); $Calculator.divide (2); -Assertequals (4, Calculator.getresult ()); -         } -  A}
test1

This is Calculatortest.java's unit test.

1  Packagecom.bxw.test;2 3 Import Staticorg.junit.assert.*;4 5 Importjava.util.Arrays;6 Importjava.util.Collection;7 8 ImportOrg.junit.Before;9 Importorg.junit.Test;Ten ImportOrg.junit.runner.RunWith; One Importorg.junit.runners.Parameterized; A Importorg.junit.runners.Parameterized.Parameters; -  - ImportCom.bxw.demo.Calculator; the /** - * Parametric testing - * Ex-class plus Runwith -  * @authorBxw +  * -  */ +@RunWith (parameterized.class) A  Public classSquaretest { at     Private StaticCalculator Calculator =NewCalculator (); -     Private intparam; -     Private intresult; - @Before -      Public voidSetUp ()throwsException { - calculator.clear (); in     } - //provide test data to@SuppressWarnings ("Rawtypes") + @Parameters -      Public StaticCollection data () { the         returnArrays.aslist (Newobject[][]{ *{2,4}, ${0,0},Panax Notoginseng{ -3,9} -         }); the     } +     //Constructor A      PublicSquaretest (intParamintresult) { the          This. param =param; +          This. result =result; -     } $ @Test $      Public voidTest () { - calculator.square (param); -System.out.println (param+ "" +result); the assertequals (result, Calculator.getresult ()); -     }Wuyi  the}
Parametric testing

Parametric testing, the test data is written in the form of a two-dimensional array.

1  Packagecom.bxw.test;2 3 Import Staticorg.junit.assert.*;4 5 ImportOrg.junit.Before;6 Importorg.junit.Test;7 ImportOrg.junit.runner.RunWith;8 ImportOrg.junit.runners.Suite;9 Ten /** One * Packaging Test A  * @authorBxw -  * -  */ the@RunWith (Suite.class) - @Suite. suiteclasses ({ -Calculatortest.class, -Squaretest.class + }) -  Public classAllTest { +      A}
Package Test

Write the test class in @suite.suiteclasses

Java Unit Test

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.