1. Install Junit(4.12), Hamcrest(1.3) with Eclipse
First, download the junit.jar and hamcrest-core.jar from the website.
Then, open the eclipse. Choose properties--java build path-libraries. Click Add External JARs. Type in the path of junit.jarand hamcrest-core.jar. add the path.
2. Install Eclemma with Eclipse
Open Eclipse. Click help-> Eclipse Marketplace and search Eclemma. Install it.
3. Write a java program for the triangle problem and test the program with Junit.
The code:
Main.java
package triangle;
public class main {
public int a,b,c;
public String xing;
public String san(int a,int b,int c){
int temp;
if(a>b){
temp=a;
a=b;
b=temp;
}
if(b>c)
{
temp=b;
b=c;
c=temp;
}
if(a>b){
temp=a;
a=b;
b=temp;
}
if(a+b<=c){
xing="not a triangle ";
return xing;
}
else{
if(a == b&&b ==c){
xing = "equilateral";
return xing;
}
else if((a == b)||(b == c)){
xing ="isosceles";
return xing;
}
else
{
xing = "scalene";
return xing;
}
}
}
}
Maintest.java
package triangle;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class mainTest {
main tra;
String xing;
@Before
public void setUp() throws Exception {
tra= new main();
}
@After
public void tearDown() throws Exception {
}
@Test
public void testequilateral(){
xing = tra.san(1, 1, 1);
assertEquals("equilateral",xing);
}
@Test
public void testisosceles(){
xing = tra.san(2, 3, 2);
assertEquals("isosceles",xing);
}
@Test
public void testscalene(){
xing = tra.san(3, 5, 4);
assertEquals("scalene",xing);
}
@Test
public void testnot(){
xing = tra.san(3, 2, 1);
assertEquals("not a triangle ",xing);
}
}
The result: