• st_lab1


    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. 

    1.安装junit 和 hamcrest

    首先在官网下载hamcrest-all-1.3和junit-4.12

    新建工程st_lab1 然后build path->configure build path->add library 和 add externel library 添加依赖项

    2.安装eclemma

    在hlep->market搜寻安装eclemma 之后选择重启

    3.测试用例

    (1)写三角形判定函数

    package st_lab1;
    
    public class Triangle {
         private int a,b,c;
         
         public Triangle(int a, int b, int c) {
                   this.a = a;
                   this.b = b;
                   this.c = c;
                }
        
        public int getKind(int a, int b, int c)
        {
           int Kind = 3;
            if(a + b <= c || a+c <= b || b+c <= a)
            {
                Kind = -1;
                return Kind;
            }
            
            if( a == b && b == c)
            {
                Kind = 0;
                return Kind;
            }
            else if(a == c || b == c || a == b)
            {
                Kind = 1;
                return Kind;
            }
            else
            {
                Kind = 2;
                return Kind;
            }
        }
    
    
    
    }

    (2)参数化用例

    import static org.junit.Assert.*;
    
    import java.util.Arrays;
    import java.util.Collection;
    
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized;
    import org.junit.runners.Parameterized.Parameters;
    
    import st_lab1.Triangle;
    
    @RunWith(Parameterized.class)
    public class TriangleTest {
    
    	private int a;
    	private int b;
    	private int c;
    	private int expected;
    	
    	private Triangle triangle;
    	
    	public TriangleTest(int expected,int a, int b,int c)
    	{
    		this.expected = expected;
    		this.a = a;
    		this.b = b;
    		this.c = c;
    	}
    	@Before
    	public void setUp() throws Exception {
    		triangle = new Triangle(a, b, c);
    	}
    
    	@After
    	public void tearDown() throws Exception {
    	}
    
    	@Parameters
    	public static Collection<Object[]> getData()
    	{
    		return Arrays.asList(new Object[][]
    				{
    					{0,2,2,2},
    					{1,5,2,5},
    					{1,2,2,3},
    					{2,1,2,3},
    					{-1,7,6,1},
    					{2,2,3,4},
    					{-1,2,5,8},
    				});	
    	}
    	@Test
    	public void testGetKind() {
    		assertEquals(this.expected, triangle.getKind(a, b, c));
    	}
    
    	
    
    }
    

    4.测试结果

  • 相关阅读:
    (转)dubbo远程调用细节
    (转)Dubbo扩展点实现细节
    (转)dubbo design
    (转) java中try/catch性能和原理
    mybatis入门基础(九)----逆向工程
    客观评价下软件培训机构
    mybatis入门基础(八)-----查询缓存
    mybatis入门基础(七)----延迟加载
    mybatis入门基础(六)----高级映射(一对一,一对多,多对多)
    为什么我不推荐大家去外包公司
  • 原文地址:https://www.cnblogs.com/Reallylzl/p/6530166.html
Copyright © 2020-2023  润新知