st-lab1

Junit、Hamcrest、Eclemma的安装和使用

1.任务要求

Tasks:

  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.

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不等边.


2.Install Junit, Hamcrest with Eclipse

   1.Junit

    Eclipse自带Junit,所以不需要添加jar包,只需要在要使用Junit的project名上,点击properties-java build path-libraries, 点击Add library,选择JunitT即可。

    如图所示,点击Add library,在弹出的对话框中选择Jnuit。

    

  2.Hamcrest 

    在网上查到Eclipse中配置hamcrest的方法与配置Junit的方法一样,然而在Eclipse的Library中并没有找到Hamcrest的库,所以采用添加jar包的方法配置。

  1. 从官网下载hamcrest-core.jar
  2. 在要使用Junit的project名上,点击properties-java build path-libraries, 点击Add External JARs,把Junit包点上就行了。

  3.Eclemma

    1.打开Eclipse,选择菜单栏“帮助” -> Eclipse Marketplace,搜索“Eclemma”,结果如图:

          

    2.点击安装即可。


 

 3.Junit、Hamcrest,Eclemma的使用

  1.Junit测试

triangle.java:

 1 package triangle;
 2 
 3 import java.util.Scanner;
 4 
 5 public class triangle {
 6     public static void main(String[] args) {
 7         System.out.println("三角形判断");
 8          @SuppressWarnings("resource")
 9         Scanner reader = new Scanner(System.in);  
10             double a=0, b=0, c=0;  
11             System.out.printf("输入a的长度:");  
12             a =reader.nextDouble();  
13             System.out.printf("输入b的长度:");  
14             b =reader.nextDouble();  
15             System.out.printf("输入c的长度:");  
16             c =reader.nextDouble();  
17             
18             System.out.println(triangle(a,b,c));
19     }
20     
21     public static String triangle(double a,double b,double c) {
22         if(a < b+c&&b < a+c&&c <a+b){  
23                 if(a==b&&b==c){  
24                     return "The triangle is equilateral.";  
25                 }  
26                 else if(a==b||a==c||c==b){  
27                     return "The triangle is isosceles.";  
28                 }  
29                 else {  
30                     return "The triangle is scalene.";  
31                 }  
32                   
33             }  
34             else{  
35                 return "构不成三角形";  
36             }  
37     }
38 
39 }

triangleTest.java:

 1 package triangle;
 2 
 3 import static org.junit.Assert.assertEquals;
 4 
 5 import org.junit.After;
 6 import org.junit.Before;
 7 import org.junit.Test;
 8 
 9 public class triangleTest {
10     String result;
11     String equilateral="The triangle is equilateral.";
12     String isosceles="The triangle is isosceles.";
13     String scalene="The triangle is scalene.";
14      
15     @Before
16      public void setUp() throws Exception {
17       System.out.println("测试开始");
18      }
19      @After
20      public void tearDown() throws Exception {
21           System.out.println("测试结束...");
22          }
23      
24 @Test
25 public void test1(){
26     result = triangle.triangle(3,4,5);
27     System.out.println(result);
28     assertEquals(scalene,result);
29 }
30 @Test
31 public void test2(){
32     result = triangle.triangle(3,3,5);
33     System.out.println(result);
34     assertEquals(isosceles,result);
35 }
36 @Test
37 public void test3(){
38     result = triangle.triangle(3,3,3);
39     System.out.println(result);
40     assertEquals(equilateral,result);
41 }
42 }

运行结果:

  2.Eclemma测试结果图:

项目覆盖为44.4%还有待提高。


 

转载于:https://www.cnblogs.com/jingyizhang/p/8645212.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值