1. install JUnit and hamcrest under Eclipse .
Pre-preparation: download JUnit and hamcrest jar packages to provide github download URLs
Https://github.com/junit-team/junit/wiki/Download-and-Install
After downloading the two jar packages, open Eclipse, create a new Java project, right-click on the Completed project folder and select the BuildPath option. Then, in the popup window, do the following: select "Libraries", click "Add External JARS", select the download path of the two jar packages above, select the jar package.
2. install eclemma under Eclipse
There are many ways to install Eclemma, and here is an online one-click installation method:
Open Eclipse, select Help->eclipsemarketplace
In the window that appears, select Search, fill in the "Eclemma" in the input box, click the Search button to display the search results, click Install directly at the search results.
After installation, restart Eclipse, when this button appears on the toolbar, the Eclemma installation is successful.
3. Write code to complete the detection of the triangle shape
According to the experimental requirements, this experiment requires testing code for a section of the code to detect a triangle shape: Given the length of the triangle three sides, to determine whether the triangle is equilateral triangle or isosceles triangle or ordinary triangles.
public class Tri {
Public String judgement (int a, int b, int c) {
if (a+b<=c | | a+c<=b | | b+c <=a) {
Return "not a triangle";
}else{
if (a==b&&a==c) {
return "equilateral";
}else if (a = = c && b! = c) {
return "Isosceles";
}else{
return "Scalene";
}
}
}
}
After the Triangle class is written, test functions are written, given the input and expected output, and tested by JUnit.
Import Org.junit.Test;
Import static org.junit.assert.*;
Import Org.junit.Before;
public class Main {
Public Tri t;
@Before
public void Create () {
t = new Tri ();
}
@Test
public void Test () {
Assertequals ("equilateral", T.judgement (1,1,1));
Assertequals ("Isosceles", T.judgement (1,2,2));
Assertequals ("Isosceles", T.judgement (2,1,2));
Assertequals ("Not a triangle", t.judgement ();
Assertequals ("Scalene", T.judgement (3,4,5));
}
}
4. Test Results
JUnit Test
Coverage test under Eclemma (code snippet)
Software Test Job (b)