The junit4.x version needs to introduce the following jar packages:
Hamcrest-core-1.3.jar
Junit-4.12-beta-3.jar
Create a new Calculator class, as follows:
Packagecom.pt; Public classCalculate {/*** addition *@paramD1 *@paramD2 *@return */ Public Static DoubleAddDoubleD1,DoubleD2) { returnD1 +D2; } /*** Subtraction *@paramD1 *@paramD2 *@return */ Public Static DoubleDiffDoubleD1,DoubleD2) { returnD1-D2; } /*** Multiplication *@paramD1 *@paramD2 *@return */ Public Static DoubleMutiDoubleD1,DoubleD2) { returnD1 *D2; } /*** Division *@paramD1 *@paramD2 *@return */ Public Static DoubleDevide (DoubleD1,DoubleD2) { returnD1/D2; }}
Calculate.java
Create a new source folder (Note: Not folder)
Create a new JUnit Test case Class (note: Not his ordinary class)
1 Packagecom.pt;2 3 4 Importjava.util.Arrays;5 Importjava.util.Collection;6 7 ImportOrg.junit.After;8 ImportOrg.junit.Before;9 Importorg.junit.Test;Ten ImportOrg.junit.runner.RunWith; One Importorg.junit.runners.Parameterized; A Importorg.junit.runners.Parameterized.Parameters; - -@RunWith (parameterized.class)//This annotation is required when specifying runner parameterized tests the Public classTestcalculate { - Private Longinput1; - Private LongInput2; - + - /** + * Constructor Function A * @paramin_1 at * @paramin_2 - */ - PublicTestcalculate (LongIn_1,Longin_2) { - This. INPUT1 =in_1; - This. Input2 =in_2; - } in -@Before//each test function executes before execution of Setup cannot be static to //@BeforeClass//only one setup must be static before testing + Public voidsetUp () { -System.out.println ("~~~~~~~~~ before starting test"); the } * $@After//every Test function executes after execution teardown cannot be staticPanax Notoginseng //@AfterClass//only once after the test is finished teardown must be static - Public voidTearDown () { theSystem.out.println ("~~~~~~~~~ after the start of the completion"); + } A the //parametric test test data + @Parameters - Public StaticCollection Predata () { $ //must be defined as long to be an error when executing the constructor $object[][] data =Newobject[][]{{1,2},{2,3},{4,5}}; - returnArrays.aslist (data);//Convert an array into a collection - } the - Wuyi @Test the Public voidTestadd () { - DoubleResult_add =Calculate.add (INPUT1, input2); WuSystem.out.println ("Addesult:" +result_add); - //asserts that if it is not the same as expected value 3, the exception is wrapped About //assert.assertequals ("Addition calculation Error", (long) 3, (long) result_add); $SYSTEM.OUT.PRINTLN ("Test Add Method:" + input1 + "+" + input2 + "=" +result_add); - } - - @Test A Public voidTestdiff () { +Calculate.diff (1, 2); theSYSTEM.OUT.PRINTLN ("Test diff method")); - } $ the @Test the Public voidTestmuti () { theCalculate.diff (1, 2); theSystem.out.println ("Test Muti method"); - } in the //If the run time is more than 300ms, the exception is reported . the@Test (timeout=300) About Public voidtestdevide () { the Try{ theThread.Sleep (298); the}Catch(Exception ex) { + - } theCalculate.diff (1, 2);BayiSystem.out.println ("Test Devide method"); the } the}
Testcalculate.java
OK;
Right-click Run as->junit Test
JUnit unit Test-parametric testing