【在myeclipse中使用Junit(4.12), Hamcrest(1.3) 和Eclemma】

一、在myeclipse中安装Junit(4.12), Hamcrest(1.3) 和Eclemma

    1.在项目根目录下点击右键-> build path ->configure build path -> library 把junit.jar, hamcrest.jar添加进去

    2.在myeclipse顶部菜单栏中 help->install from catalog,在搜索栏中输入Eclemma,点击安装,一步步按照提示,即可完成安装

二、判断三角形的程序

    1、程序要求

    输入三条边的长度,判断该三角形是等边三角形、不等边三角形或者等腰三角形

    2.程序实现

    public class Triangle {

   

    public int trian(int a,int b,int c){

       if((a==b)&&(a==c)&&(b==c)){  //等边

           return 0;

       }

       else if((a != b)&&(a!=c)&&(b!=c)){  //不等边

           return 1;

       }

       else{

           return 2;      //等腰

       }

    }

等边三角形时,返回0;不等边三角形时返回1;等腰三角形时返回2

 

三、测试程序

import java.util.Arrays;

import java.util.Collection;

import org.junit.*;

import org.junit.runner.RunWith;

import org.junit.runners.Parameterized;

import org.junit.runners.Parameterized.Parameters;

import static org.junit.Assert.*;

 

@RunWith(Parameterized.class) //这是一个参数化的测试类

public class triangleTest {

private Triangle tr;

private int a,b,c,expected;

 

public triangleTest(int a,int b,int c,int expected){

    this.a=a;

    this.b=b;

    this.c=c;

    this.expected=expected;

}

 @Before   //在运行之前先运行这个函数

public void setUp(){

    tr=new Triangle();

}

 

 @Parameters  //给构造函数参数初始化

 public static Collection<Object[]> getData(){

     return Arrays.asList(new Object[][]{

            {1,2,3,1},

            {2,2,2,0},

            {2,2,3,2},

            {2,3,4,1}

     });

 }

@Test     //测试Train函数

public void testTrian(){

    assertEquals(this.expected,tr.trian(a,b,c));

}

 

}

四、实验结果

1.四个测试用例中全部通过

2.覆盖域

posted @ 2016-03-19 18:08  沿途风景  阅读(861)  评论(0编辑  收藏  举报