Software testing lab1 -- Junit and Eclemma

1.     Install Junit(4.12), Hamcrest(1.3) with Eclipse

 

First create a java project (eg.lab1), and then create a class (eg.triangle).Then right click on lab1-Build Path-Add Extend Archives (As shown below).

Then select the corresponding junit and hamcrest jar packages.

 

2.     Install Eclemma with Eclipse

 

Decompression the Eclemma file in the dropins directory.

 

And then choose help-install new software。

 

Select the appropriate directory.

 

and then click next-finish.

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.

 

Java code:

 

 1 package lab1;
 2 
 3 public class triangle {
 4     public triangle(){
 5         
 6     }
 7     public String triangles(int a, int b, int c) {
 8         if(a + b > c && a + c > b && b + c > a)    
 9         {
10             if(a == b && b == c)
11                 return "equilateral";
12             else if(a == b || b == c || a == c)
13                 return "isosceles";
14             else {
15                 return "scalene";
16             }
17         }
18         else {
19             return "Non-triangular";
20         }
21     }
22 }

 

 

 

Junit test code:

 1 package lab1;
 2 
 3 import static org.junit.Assert.*;
 4 import static org.hamcrest.Matchers.*;
 5 
 6 import org.junit.Before;
 7 import org.junit.Test;
 8 
 9 public class triangleTest {
10     public triangle tr = new triangle();
11     @Before
12     public void setUp() throws Exception {
13     }
14 
15     @Test
16     public void testTrangle() {
17         assertEquals("equilateral", tr.triangles(3, 3, 3));
18         assertEquals("isosceles", tr.triangles(3, 2, 2));
19         assertEquals("scalene", tr.triangles(3, 4, 2));
20         assertEquals("Non-triangular", tr.triangles(1, 1, 2));
21         //fail("Not yet implemented");
22     }
23 
24 }

The operation result:

 

 

posted @ 2017-03-10 15:14  Jerome文  阅读(112)  评论(0编辑  收藏  举报