如何使用eclipse进行junit测试
1个回答
2017-11-12
展开全部
首先导入junit的jar包,
写一个类:
/**
* java做<a href="https://www.baidu.com/s?wd=%E5%8A%A0%E5%87%8F%E4%B9%98%E9%99%A4&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4ryNhuHmLuWI-n16dn1NW0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHf4PWnzPjD4nHRYPHbYrjTLrf" target="_blank" class="baidu-highlight">加减乘除</a>
* @author young
*
*/
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) throws Exception {
if (0 == b) {
throw new Exception("除数不能为零");
}
return a / b;
}
}
再新建一个JUnit测试类:
import junit.framework.TestCase;
/**
* JUnit<a href="https://www.baidu.com/s?wd=%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4ryNhuHmLuWI-n16dn1NW0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHf4PWnzPjD4nHRYPHbYrjTLrf" target="_blank" class="baidu-highlight">单元测试</a>
* @author young
*
*/
public class CalculatorTest extends TestCase {
private Calculator cal;
protected void setUp() throws Exception {
cal = new Calculator();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testAdd() {
int result = cal.add(1, 2);
System.out.println("add: " + result);
}
public void testMinus() {
int result = cal.minus(5, 2);
System.out.println("minus: " + result);
}
public void testMultiply() {
int result = cal.multiply(4, 2);
System.out.println("multiply: " + result);
}
public void testDivide() {
int result = 0;
try {
result = cal.divide(10, 5);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("divide: " + result);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询