Lab-1

实验要求:

  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. 
  4. Description of triangle problem: 
  5. Function triangle takes three integers a,b,c which are length of triangle sides; calculates whether the triangle is equilateral, isosceles, or scalene.

     1.手动安装Junit(4.12), Hamcrest(1.3)

        在网上下载相关jar包

          右击project,点击properties,在弹出的面板中选择Java Build Path

          选择Libraries, 点击Add External JARs, 找到Junit-4.12.jar 及hamcrest-all-1.3.jar包的路径,选择这两个jar包后选择即可 

          编写Triangle代码:

package test.cn;

public class Triangle {
         public String triangle(int a,int b,int c){
        	 if(a+b<=c || a+c<=b || b+c<=a){
        		 return "non-triangle";
        	 }
        	 else if(a==b && b==c){
        		 return "equilateral";
        	 }
        	 else if(a==b || b==c || a==c){
        		 return "isosceles";
        	 }
        	 else return "scalene";
        	 
         }
}

 最好新建一个package放测试类,并在新文件中引入要测试的类包,测试类名一般规定在被测试的类后加Test, 在Class Under Test里搜索要测试的类如下图

测试类代码,注意引入test.cn.Triangle;

package test.cn.tju;

import static org.junit.Assert.*;

import org.junit.Test;

import test.cn.Triangle;

public class TriangleTest {
   
	@Test
	public void testTriangle() {
		 Triangle t = new Triangle();
		assertEquals("non-triangle", t.junit(1,2,3));
		assertEquals("equilateral", t.junit(2,2,2));
		assertEquals("isosceles", t.junit(2,2,3));
		assertEquals("scalene", t.junit(4,2,3));
	}

}

  

          注意注意!!!在运行测试前一定要保存所有更改,不像之前的java小程序,直接run就保存了,一定要Save一下,否则会出错的!!!

          最后右击文件Run->Junit test就可以

 

 

 

 

2.手动安装EclEmma

      1.下载最新的Eclemma zip包。

      2.解压到Eclipse安装目录的dropins目录,目录结构如下

                                  

在Eclemma 的解压目录中会有META-INF 、plugins 、features 三个文件夹,以及content.jar 、artifacts.jar 两个jar包。

注意eclipse4.5版本的需要删除META-INF 才可以,重启eclipse出现    即可

测试覆盖率:

 

 

原文地址:https://www.cnblogs.com/atongmumu/p/6535107.html