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:
Package JUnit; Public classTriangle {Private Static intEquilateral =1;Private Static intIsosceles =2;Private Static intScalene =3;Private Static intError =4; Public intgetequilateral () {returnequilateral; } Public intGetisosceles () {returnisosceles; } Public intGetscalene () {returnScalene; } Public intGetError () {returnerror; } Public intTypeintAintBintc) { if(a<=0|| b<=0|| c<=0) { returnerror; } Else if(a+b<c| | a+c<b| | b+c<a) {returnerror; } Else if(a!=b&&b!=c&&a!=c) {returnScalene; } Else if(a==b&&b==c) {returnequilateral; } Else if(a==b| | a==c| | b==c) {returnisosceles; } return 0;}}
2. Test:
Package Junit;import Org.junit.test;importStaticorg.junit.assert.*; Public classTriangletest {PrivateTriangle triangle =Newtriangle (); @Test Public voidTestnottriangel () {assertequals (Triangle.geterror (), Triangle.type (0,2,3)); } @Test Public voidTestscalene () {assertequals (Triangle.getscalene (), Triangle.type (3,2,4)); } @Test Public voidTestisosceles () {assertequals (Triangle.getisosceles (), Triangle.type (3,2,3)); } @Test Public voidtestequilateral () {assertequals (Triangle.getequilateral (), Triangle.type (3,3,3)); }}
Three. Test results:
All four test cases pass, coverage 94.9%
Software testing first time on the machine Hu Xiao