import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import MyMath;
//测试方法:带有@Test注解。方法是public的,没有返回值和参数的
public class MyMathTest {
private static MyMath mm;
@BeforeClass
public static void bb(){
mm = new MyMath();
// System.out.println("bb");
}
@AfterClass
public static void aa(){
mm = null;
// System.out.println("aa");
}
// @Before//每个测试方法开始前执行
// public void b1(){
// mm = new MyMath();
// System.out.println("before");
// }
// @After
// public void a1(){
// mm = null;
// System.out.println("after");
// }
@Test
public void testAdd() {
// MyMath mm = new MyMath();
int result = mm.add(1, 2);
assertEquals(3, result);
}
@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 testDivide1() {
// MyMath mm = new MyMath();
mm.divide(10, 0);
}
//测试执行效率:运行时间不能超过1秒钟
@Test(timeout=1000)
public void testDivide2(){
// MyMath mm = new MyMath();
int result = mm.divide(10, 2);
assertEquals(5, result);
}
}
Junit简单复习
最新推荐文章于 2024-04-22 19:51:52 发布