3014218146_段义海_Lab1,第一次上机,学习使用JUnit,Hamcrest,Eclemma

本文介绍了一个使用JUnit进行单元测试的实例,通过编写一个判断三角形类型的程序,并使用JUnit 4.12和Hamcrest 1.3进行测试。文章详细记录了项目的构建过程,包括安装JUnit和Hamcrest、配置Eclemma进行代码覆盖率分析等步骤。
 this is the first time i learn how to use JUnit, it's a significative experience.

1. Build a project

    Write the code,the code is not very difficult. I divided it into four conditions. not a triangle, equilateral triangle, isosceles triangle and scalene triangle.

package lib1;



public class Triangle {
    public static String triganles (int a, int b, int c){
        if(a+b > c && a+c > b && b+c > a){
        if (a == b && b == c)
            return "this is a equilateral triganle!";
        else if (a == b || b == c || c == a)
            return "this is a isosceles triganle!";
        else
            return "this is a scalene triganle!";
            
        }
        else
        return "this is not triganle!";
    }
    
    
}

2.Install Junit(4.12), Hamcrest(1.3) with Eclipse

  A)   download Junit(4.12), Hamcrest(1.3)   (eclipse has its own JUnit, but in order to pritace adding a external JARs, I used this method.)

  B)   Click the project with the right button, select properties ->Java Build Path ->Libraries->Add external JARs,

Then select the junit-4.12.jar and Hamcrest-core-1.3

3. Install Eclemma

  a)  Put the Eclemma.zip under the directory D:\Program Files\eclipse\eclipse\dropins ,decompress it.

  b)

  Click help->install new software->add

  

  Select the right path

  Ok! 

4.Add a source folder which will contain test package and the test class.

  

   it has the same package name as the package in src.

5. Write test code.

package lib1;

import static org.junit.Assert.*;

import org.junit.Test;

public class TestTriangle {

    @Test
    public void testTriangle() {
        assertEquals("this is not triganle!",new Triangle().triganles(1,2,3));
        assertEquals("this is a equilateral triganle!",new Triangle().triganles(1,1,1));
        assertEquals("this is a isosceles triganle!",new Triangle().triganles(2,2,3));
        assertEquals("this is a scalene triganle!",new Triangle().triganles(2,3,4));
    }

}

6. Run it as JUnit test and use eclemma.

The coverage is 100%.

Then i put it on GIT.

 

转载于:https://www.cnblogs.com/duanyihai/p/6531698.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值