I. Installation of JUnit, Hamcrest, and Eclemma:
1.junit and Hamcrest add Junit-4.12.jar and Hamcrest-all-1.3.jar in build path by adding external jars after creating a new JUNITHW1 project.
2.eclemma Click Help-eclipse marketplace-find-eclemma Install under Eclipse.
Two. Write Triangle and Triangletest:
1. Judging triangles:
Packageexample;/*** Created by Ev_eraser on 2016/3/18.*/ Public classMyclass { PublicString Triangle (intAintBintc) {if(A + b < C | | A + c < b | | B + C <a)return"Nottriangle"; if(A = = b && b = =c)return"Isosceles"; if(A = = B | | b = = c | | a = =c)return"Equilateral"; Else return"Scalene"; }}
2. Test:
Packageexample;ImportOrg.junit.Before;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.junit.runners.Parameterized;Importjava.util.Arrays;Importjava.util.Collection;Import Staticorg.junit.Assert.assertEquals; the @RunWith (parameterized.class) Public classMyclasstest {PrivateMyclass Myclass; Private intinput1; Private intInput2; Private intINPUT3; PrivateString expected; PublicMyclasstest (intINPUT1,intInput2,intInput3, String expected) { This. INPUT1 =input1; This. Input2 =Input2; This. INPUT3 =INPUT3; This. Expected =expected; } @Before Public voidSetUp ()throwsException {myClass=NewMyclass (); } @Parameterized. Parameters Public StaticCollection<object[]>GetData () {returnArrays.aslist (Newobject[][]{{2, 2, 2, "Isosceles"}, {2, 2, 3, "equilateral"}, {2, 4, 3, "Scalene"}, {2, 9, 2, "Nottriangle"} }); }//@After//Public void TearDown () throws Exception {//// }@Test Public voidTesttriangle ()throwsException {assertequals ( This. Expected, Myclass.triangle (INPUT1,INPUT2,INPUT3)); }}
Three. Test results:
Software test first time on machine