zoukankan      html  css  js  c++  java
  • 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.测试结果

  • 相关阅读:
    【Java】Junit快速入门
    【Redis】Redis Sentinel 哨兵模式搭建
    【Redis】Redis 主从模式搭建
    Android开发过程中的坑及解决方法收录(六)
    杂牌机搞机之旅(一)——获得root权限(刷入magisk)
    Java 学习笔记 泛型
    Java 学习笔记 反射与迭代器
    Java 学习笔记 正则表达式
    Java 学习笔记 执行外部命令 包装类 枚举类型
    IDEA设置显示中文文档API方法说明
  • 原文地址:https://www.cnblogs.com/Reallylzl/p/6530166.html
Copyright © 2011-2022 走看看