Lab 1 Junit and Eclemma - GhostDragon007/softwaretesting GitHub Wiki

Tasks:

  • 1.Install Junit(4.12), Hamcrest(1.3) with Eclipse
  • 2.Install Eclemma with Eclipse
  • 3.Write a java program for the triangle problem and test the program with Junit.
    • a) Description of triangle problem: Function triangle takes three integers a,b,c which are length of triangle sides; calculates whether the triangle is equilateral, isosceles, or scalene.

Solutions:

1.Triangle.java (Original Code)

package sjh;

public class triangle { public double a, b, c; public triangle(){ this.a = 1; this.b = 1; this.c = 1; } public void setSide(double a, double b, double c){ this.a = a; this.b = b; this.c = c; try{ if(a>0&&b>0&&c>0&&a+b>c&&b+c>a&&a+c>b){ //nothing will happen } else{ //not a triangle throw new Exception(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public boolean ifEquilateral(){ if(a == b && b == c){ return true; } else { return false; } } public boolean ifIsosceles(){ if(a == b || b == c || a == c){ return true; } else { return false; } } public boolean ifScalene(){ if(a != b && b != c && a != c){ return true; } else { return false; } } }

2. triangleTest.java (Testing Code)

package sjh;

import static org.junit.Assert.*;

import org.junit.After; import org.junit.Before; import org.junit.Test;

public class triangleTest { private static triangle tri = new triangle();

`@Before`
`public void setUp() throws Exception {`
	
`}`

`@After`
`public void tearDown() throws Exception {`

`}`

`@Test`
`public void testIfEquilateral() {`
	`tri.setSide(1.0, 1.0, 1.0);`
	`assertEquals(tri.ifEquilateral(), true);	`
`}`

`@Test`
`public void testIfIsosceles() {`
	`tri.setSide(1.5, 2.0, 2.0);		`
	`assertEquals(tri.ifIsosceles(), true);`

`}`

`@Test`
`public void testIfScalene() {`
	`tri.setSide(1.0, 1.5, 2.0);`
	`assertEquals(tri.ifScalene(), true);`
`}`

}

Result:

All Succeeded!