Junit4入门参考手册..docVIP

  • 88
  • 0
  • 约3.75千字
  • 约 6页
  • 2018-01-02 发布于河南
  • 举报
Junit4入门参考手册.

Junit4入门参考手册.在实际运用中,借助Eclipse或者MyEclipse工具,将Junit的包导入到工程中即可。(other(junit test case.即可看到如下界面。 点击Finish后会报错,原因是junit包未和工程关联。在报错部分点击红叉,然后选中要添加的包即可。 以下部分内容,只对注解及部分参数使用进行说明。 public class Calculator { ?private static int result; // 静态变量,用于存储运行结果 ??? public void add(int n) { ??????? result = result + n; ??? } ??? public void substract(int n) { ??????? result = result - 1;? //Bug: 正确的应该是 result =result-n ??? } ??? public void multiply(int n){ ??? }???????? // 此方法尚未写好 ??? public void divide(int n){ ??????? result = result / n; ??? } ??? public void square(int n){ ??????? result = n * n; ??? } ??? public void squareRoot(int n) { ??????? for (; ;) ;??????????? //Bug : 死循环 ??? } ??? public void clear(){// 将结果清零 ??????? result = 0; ??? } ??? public int getResult(){ ??????? return result; ??? } } import static org.junit.Assert.*; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.Description; import org.junit.runner.RunWith; import org.junit.runner.Runner; import org.junit.runner.notification.RunNotifier; /** ?* @author Administrator ?* ?*/ public class CalculatorTest{ ? ?//每个测试类只能有一个方法被标注为@BeforeClass 或 @AfterClass, ?//并且该方法必须是Public和Static的 ?// @Test(timeout = 1000)防止死循环 设定测试的超时时间 ? //@Test(expected = ArithmeticException.class) ?private static Calculator calculator = new Calculator(); ?/** ? * @throws java.lang.Exception ? * before方法是一个复原方法 会将各部分测试的值独立开来 ? */ ?@Before ?public void setUp() throws Exception { ??//将结果清0 ??calculator.clear(); ?} ?/** ? * Test method for {@link com.my.calculator.Calculator#add(int)}. ? */ ?@Test ?public void testAdd() { ??//fail(Not yet implemented); ??calculator.add(2); ??calculator.add(3); ??assertEquals(5, calculator.getResult()); ???? ?} ?/** ? * Test method for {@link com.my.calculator.Calculator#substract(int)}. ? */ ?@Test ?public void testSubstract() { ??//result作为全局变量经过上面的相加运算后变成了15 ??//减法实为15-1 所以结果应该是14 ??calculator.add(10); ??calculator.substract(2); ??assertEquals(

文档评论(0)

1亿VIP精品文档

相关文档