在Eclipse中使用JUnit4进行单元测试(初级篇)

发表于:2010-1-15 14:09

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:51Testing软件测试网采编

分享:

  点击“下一步”后,系统会自动列出你这个类中包含的方法,选择你要进行测试的方法。此例中,我们仅对“加、减、乘、除”四个方法进行测试。如下图所示:

  之后系统会自动生成一个新类CalculatorTest,里面包含一些空的测试用例。你只需要将这些测试用例稍作修改即可使用。完整的CalculatorTest代码如下:

  package andycpp;
  import static org.junit.Assert.*;
  import org.junit.Before;
  import org.junit.Ignore;
  import org.junit.Test;
  public class CalculatorTest ...{
  private static Calculator calculator = new Calculator();
  @Before
  public void setUp() throws Exception ...{
  calculator.clear();
  }
  @Test
  public void testAdd() ...{
  calculator.add(2);
  calculator.add(3);
  assertEquals(5, calculator.getResult());
  }
  @Test
  public void testSubstract() ...{
  calculator.add(10);
  calculator.substract(2);
  assertEquals(8, calculator.getResult());
  }
  @Ignore("Multiply() Not yet implemented")
  @Test
  public void testMultiply() ...{
  }
  @Test
  public void testDivide() ...{
  calculator.add(8);
  calculator.divide(2);
  assertEquals(4, calculator.getResult());
  }
  }

43/4<1234>
价值398元的测试课程免费赠送,填问卷领取吧!

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计

法律顾问:上海漕溪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2022
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号