Junit 测试

来源:互联网 发布:端口号 进程 编辑:程序博客网 时间:2024/06/13 21:15
  1. 通常新建一个test包,eclipse中已经导入了junit测试的包,在测试方法中进行相应的调用实现相应的测试。
package com.itheima.test;import static org.junit.Assert.assertEquals;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;import com.itheima.MyMath;//单元测试public class MyMathTest {    private static MyMath mm;//  @Before  //每个测试方法执行前自动调用    @BeforeClass //加载类的字节码时就执行,只执行一次    public static void init(){        mm = new MyMath();    }//  @After    @AfterClass    public static void destory(){        mm = null;    }    /*     * 测试方法:用于测试的方法     * 1、必须是pulbic的     * 2、必须没有返回值     * 3、方法参数为空     * 4、有@Test注解     */    @Test    public void testAdd() {//      MyMath mm = new MyMath();        int result = mm.add(1, 2);        // 断言        assertEquals(3, result);//期望值和实际运行结果进行比对。成功,绿色的bar    }    @Test    public void testDivide() {//      MyMath mm = new MyMath();        int result = mm.divide(10, 2);        assertEquals(5, result);    }    //测试异常    @Test(expected=java.lang.ArithmeticException.class)    public void testException() {//      MyMath mm = new MyMath();        mm.divide(10, 0);    }    //测试方法的执行效率    @Test(timeout=100)//即使期望值和实际值相同的。超出运行时间,也是失败的。time指定的值为毫秒值。    public void testEfficiency() {//      MyMath mm = new MyMath();        int result = mm.add(1, 2);        // 断言        assertEquals(3, result);    }}

转载自黑马程序员的课程笔记。

1 0
原创粉丝点击