lab1
Tasks:
- Install Junit(4.12), Hamcrest(1.3) with Eclipse
- Install Eclemma with Eclipse
- 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.
Requirements for the experiment:
- Finish the tasks above individually.
- Check in your java code and junit test program to github and send the URL to tjuscsst@qq.com
- Post your experiment report to your blog and send the URL to tjuscsst@qq.com , the following information should be included in your report:
a) The brief description that you install junit, hamcrest and eclemma.
b) The test result and coverage report (printscreen) of your tests on triangle problem.------------------------------------------------------------------------------------------------------------------
一.添加junit-4.12.jar, hamcrest-all-1.3.jar 和hamcrest
1.在eclipse中新建一个项目,建立如图所示的项目结构
通过build path 添加junit-4.12.jar, hamcrest-all-1.3.jar到项目中。
2 安装 eclemma插件
help—>eclipse marketplace
如下图搜 eclemma 点击installed
二。
在src和test中建立相同名称的包(shang)
编写lab1类和测试类testlab
package shang;
public class Lab1 {
public String triangle(int a, int b, int c){
String str;
if((a+b) <= c || (a+c)<= b || (b+c)<= a ){
str = new String("It is not a triangle");
return str;
}
else if(a==b && b==c){
str = new String("The triangle is equilateral");
return str;
}
else if(a==b || b==c || a==c){
str = new String("The triangle is isosceles");
return str;
}
else
str = new String("The triangle is scalene");
return str;
}
}
package shang;
import static org.junit.Assert.*;
import org.junit.Test;
public class TestLab1 {
private Lab1 lab1;
private Lab1 lab2;
private Lab1 lab3;
private Lab1 lab4;
@Test
public void testTriangle(){
lab1 = new Lab1();
assertEquals((new String("It is not a triangle")),lab1.triangle(1,1 ,2 ));
lab2 = new Lab1();
assertEquals((new String("The triangle is equilateral")),lab2.triangle(1,1 ,1));
lab3 = new Lab1();
assertEquals((new String("The triangle is isosceles")),lab3.triangle(2,2 ,3 ));
lab4 = new Lab1();
assertEquals((new String("The triangle is scalene")),lab4.triangle(2,3 ,4 ));
}
}
运行结果: