首页 > 代码库 > st lab1: junit and eclemma

st lab1: junit and eclemma

1.install junit and eclemma

在网上搜素并下载junit-4.12.jar 和 hamcrest-core-1.3.jar 两个jar包,在项目里创建一个lib文件夹将jar包放进去,再右键选择这两个jar包,选择Build Path->add to build path 即可。

安装eclemma:

点击eclipse的help,选择eclipse marketplace, 在find框里搜索eclemma并按照指示安装即可。

2.lab code

package test1;

import java.util.Arrays;

public class triangle {
    private static String result = "";
    private static int[] length = new int[3];
    
    public void clearArray(){
        for(int i=0;i<3;i++){
            length[i] = 0;
        }
    }
    
    public static void getIntegers(int a,int b,int c){
        length[0] = a;
        length[1] = b;
        length[2] = c;
        Arrays.sort(length);
    }
    
    public static void judgeIllegal(){
        if(length[0] + length[1] <= length[2]){
            result = "illegal";
        }
    }
    
    public static void judgeEquilateral(){
        if(length[0] == length[1] && length[1] == length[2]){
                result = "equilateral";
            }
    }
    
    public static void judgeIossceles(){    
        if(length[0] == length[1] || length[1] == length[2]){
                result = "isosceles";
            }
    }
    
    public static void judgeScalene(){
        if(length[0] != length[1] && length[1] != length[2]){
            result = "scalene";
        }
    }
    
    public static String getResult(){
        return result;
    }
    
}
package test1;

import static org.junit.Assert.*;

import org.junit.Test;

public class triangleTest {

	@Test
	public void testJudgeIllegal() {
		triangle.getIntegers(3, 3, 7);
		triangle.judgeIllegal();
		assertEquals("illegal", triangle.getResult());
	}

	@Test
	public void testJudgeEquilateral() {
		triangle.getIntegers(3, 3, 3);
		triangle.judgeEquilateral();
		assertEquals("equilateral", triangle.getResult());
	}

	@Test
	public void testJudgeIossceles() {
		triangle.getIntegers(3, 3, 4);
		triangle.judgeIossceles();
		assertEquals("isosceles", triangle.getResult());
	}

	@Test
	public void testJudgeScalene() {
		triangle.getIntegers(3, 4, 5);
		triangle.judgeScalene();
		assertEquals("scalene", triangle.getResult());
	}

}

  

st lab1: junit and eclemma