开发工具Eclipse

一、编辑被测试的java类

public class test {
	public int add(int a, int b) {
        return a+b;
    }

    public int substract(int a, int b) {
        return a-b;
    }

    public int multiply(int a, int b) {
        return a*b;
    }

    public int divide(int a, int b) {
        return a/b;
    }

}

二、导入Junit

三、编写测试类

import static org.junit.Assert.*;
import org.junit.Test;
public class ttest {
	@Test
	public void add(){
		assertEquals(12,new test().add(8, 4));
	}
	@Test
	public void substract(){
		assertEquals(4,new test().substract(8, 4));
	}
	@Test
	public void multiply(){
		assertEquals(32,new test().multiply(8, 4));
	}
	@Test
	public void divide(){
		assertEquals(2,new test().divide(8, 4));
	}

}

四、测试类运行结果

(1)Runs:表示总共有几个测试方法,已经运行了几个

(2)Errors:表示抛出异常的测试方法的个数

(3)Failures:表示失败的测试方法的个数

posted on 2018-03-21 17:07  大甜啊  阅读(164)  评论(0编辑  收藏  举报