在Eclipse中配置Junit的方法:
1、 在要使用Junit的project名上,点击properties--java build path-libraries, 点击Add library,选择Junit即可。点击Add library,在弹出的对话框中选择Jnuit,接下来选择junit版本即可。
public class Calculator {
public int add(int a,int b)
return a+b;
}
public int minus(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.Before;
import org.junit.Test;
public class CalculatorTest {
@Before
public void setUp() throws Exception {
}
@Test
public void testAdd() {
//实例化一个对象
Calculator c = new Calculator();
//对象调用被侧方法及传参
int result = c.add(2, 5);
//使用断言比较预期结果和实际结果
assertEquals(7,result,0.0);
//Assert.assertEquals(7, result);
//fail("Not yet implemented");
}
@Test
public void testMinus() {
//fail("Not yet implemented");
Calculator c = new Calculator();
int result = c.minus(2, 5);
assertEquals(-3,result,0.0);
}
@Test
public void testMultiply() {
//fail("Not yet implemented");
Calculator c = new Calculator();
int result = c.multiply(2, 5);
assertEquals(10,result,0.0);
}
@Test
public void testDivide() {
//fail("Not yet implemented");
Calculator c = new Calculator();
int result = c.divide(10, 5);
assertEquals(2,result);
}
}
标签:int,Calculator,四则运算,void,result,Test,public From: https://www.cnblogs.com/youlinaixu/p/17198061.html