软件工程第二次作业 junit测试

软件工程第二次作业---junit测试

1.安装eclipse工具

  • 下载 Java JDK
    enter image description here
  • 安装 JDK
    enter image description here
  • 下载 eclipse
    enter image description here
  • 安装 eclipse
    enter image description here

2.测试工具junit

  • 测试加减乘除
    
public class Calculate {

    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) {
        // 这里只是最简单的测试demo,暂不做容错处理
        return a / b;
    }
}
  • 建立junit test

enter image description here

  • 导入junit测试

enter image description here

  • 对calculate类中的加、减、乘、除四个方法进行测试的测试代码
import static org.junit.Assert.*;

import org.junit.Test;

public class CalculateTest {

	@Test
	public void add(){
	    assertEquals(100, new Calculate().add(95, 5));
	}

	@Test
	public void substract(){
	    assertEquals(0, new Calculate().substract(100, 100));
	}

	@Test
	public void multiply(){
	    assertEquals(1, new Calculate().multiply(1, 1));
	}

	@Test
	public void divide(){
	    assertEquals(0, new Calculate().divide(100, 101));
	}

}

  • 测试结果

enter image description here

结果正确

经过学习junit测试令我掌握很多学习经验。

posted on 2018-03-22 13:29  张子扬  阅读(188)  评论(0编辑  收藏  举报

导航