JUnit 傻瓜教程
添加一個java工程
點擊右鍵選擇Properties
创建一个source folder 目的就是把测试类和被测试的类分开
添加一个类 加法 乘法
public class MathOperation {
public int add(int a,int b){
int result=a+b;
return result;
}
public int multiply(int a,int b){
int result = a * b;
return result;
}
}
添加一个测试类
生成的测试类中添加断言
public class MathOperationTest {
MathOperation mo=new MathOperation();
@Test
public void testAdd() {
//fail("Not yet implemented");
int expected =3;
int value= mo.add(1, 2);
assertEquals(expected, value);
}
@Test
public void testMultiply() {
//fail("Not yet implemented");
int expected =4;
int value= mo.multiply(2, 2);
assertEquals(expected, value);
}
}
run as JUnit test
时间: 2024-10-07 05:29:46