【在myeclipse中使用Junit(4.12), Hamcrest(1.3) 和Eclemma】
2016-03-19 18:08
453 查看
一、在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.覆盖域
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.覆盖域
相关文章推荐
- java第六节-this关键字,java的内部类
- Java线程之间的通信-等待/通知机制
- Java设计模式 -- 工厂方法模式
- java.lang.IncompatibleClassChangeError
- java内存模型
- Java反射机制<2>
- [Java语言] Sort Colors 颜色排序
- [Java代码] [Leetcode] Combination Sum 组合数之和
- Java集合总结
- [Java语言] Same Tree Symmetric Tree 相同树 对称树
- 回溯法解决n后问题
- 【WebService】——CXF整合Spring
- Struts2 搭建一个简单登录应用
- Struts2 Result-type(封装Action层到View层的跳转逻辑)
- Java命名规则详细总结
- (java) 四则运算(二)
- java操作properties配置文件
- java操作properties配置文件
- java中包命名常见规则
- Spring+Quartz的版本问题