之前一篇博文(JUnit基础及第一个单元测试实例(JUnit3.8))介绍了用JUnit做单元测试的基本方法,并写了一个简单的类Calculator,其中包含了整型加减乘除的简单算法。

  本文通过完善其中的除法和除法的单元测试来继续说明JUnit的用法。

  首先完善Calculator类中的除法,在除数为零的情况下抛出一个异常:

    public int divide(int a, int b) throws Exception
{
if(0 == b)
{
throw new Exception("除数不能为0");
}
return a / b;
}

设计测试用例

  测试用例中不再抛出异常,而是使用try catch块。

  首先是测试正常情况的测试用例:

    public void testDivide()
{
int result = 0;
try
{
result = calculator.divide(12, 3);
}
catch (Exception e)
{
e.printStackTrace(); // 如果抛出异常,证明测试失败,没有通过,没通过的测试计数在Failures中
Assert.fail();
// 如果不加这一行,如果程序进入到catch,无法判断其失败
}
// 判断方法的返回结果
Assert.assertEquals(4, result);// 第一个参数是期望值,第二个参数是要验证的值 }

  测试异常情况的测试用例:

    public void testDivideByZero()
{
Throwable tx = null; int result = 0;
try
{
result = calculator.divide(12, 2); Assert.fail("没有抛出异常,测试失败");// 如果执行到这行代码,则证明没有抛出异常,说明我们的验证失败
}
catch (Exception e)
{
e.printStackTrace();
tx = e; } Assert.assertEquals(Exception.class, tx.getClass());// 抛出的异常类型是否和期望一致
Assert.assertEquals("除数不能为0", tx.getMessage());// 抛出的异常信息是否和期望一致 //如果上面两个都通过,则测试通过 }

  

  此代码中故意将除数改为2,测试失败。

  除数为2时的执行情况:

  

  总结:Assert.fail()加在期望中不可能到达的地方,一旦到达,表明测试失败,结果与预期不同。

附上完整代码:

目标类:

Calculator.java 

package com.mengdd.junit;

public class Calculator
{
public int add(int a, int b)
{
return a + b;
} public int subtract(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("除数不能为0");
}
return a / b;
}
}

测试类:

CalculatorTest.java

package com.mengdd.junit;

import junit.framework.Assert;
import junit.framework.TestCase; public class CalculatorTest extends TestCase
{ private Calculator calculator = null; @Override
public void setUp() throws Exception
{
System.out.println("set up");
// 生成成员变量的实例
calculator = new Calculator();
System.out.println(calculator);
} @Override
public void tearDown() throws Exception
{
System.out.println("tear down");
} public void testAdd()
{
int result = calculator.add(1, 2);
// 判断方法的返回结果
Assert.assertEquals(3, result);// 第一个参数是期望值,第二个参数是要验证的值
} public void testSubtract()
{
int result = calculator.subtract(1, 2);
// 判断方法的返回结果
Assert.assertEquals(-1, result);// 第一个参数是期望值,第二个参数是要验证的值 } public void testMultiply()
{
int result = calculator.multiply(2, 3);
// 判断方法的返回结果
Assert.assertEquals(6, result);// 第一个参数是期望值,第二个参数是要验证的值 } public void testDivide()
{
int result = 0;
try
{
result = calculator.divide(12, 3);
}
catch (Exception e)
{
e.printStackTrace(); // 如果抛出异常,证明测试失败,没有通过,没通过的测试计数在Failures中
Assert.fail();
// 如果不加这一行,如果程序进入到catch,无法判断其失败
}
// 判断方法的返回结果
Assert.assertEquals(4, result);// 第一个参数是期望值,第二个参数是要验证的值 } public void testDivideByZero()
{
Throwable tx = null; int result = 0;
try
{
result = calculator.divide(12, 0); Assert.fail("没有抛出异常,测试失败");// 如果执行到这行代码,则证明没有抛出异常,说明我们的验证失败
}
catch (Exception e)
{
e.printStackTrace();
tx = e; } Assert.assertEquals(Exception.class, tx.getClass());// 抛出的异常类型是否和期望一致
Assert.assertEquals("除数不能为0", tx.getMessage());// 抛出的异常信息是否和期望一致 //如果上面两个都通过,则测试通过 } }

JUnit3 结合一个除法的单元测试说明Assert.fail()的用法的更多相关文章

  1. 单元测试中Assert类的用法

    Assert类所在的命名空间为Microsoft.VisualStudio.TestTools.UnitTesting 在工程文件中只要引用Microsoft.VisualStudio.Quality ...

  2. VS单元测试中Assert类的用法

    首先说介绍一下,Assert类所在的命名空间为Microsoft.VisualStudio.TestTools.UnitTesting 在工程文件中只要引用Microsoft.VisualStudio ...

  3. 单元测试中Assert类

    一.Assert类的使用 1.Assert类所在的命名空间为Microsoft.VisualStudio.TestTools.UnitTesting 在工程文件中只要引用Microsoft.Visua ...

  4. 【php】assert函数的用法

    [php]assert函数的用法 http://www.douban.com/note/217557007/ 2012-06-01 10:32:37   assert这个函数在php语言中是用来判断一 ...

  5. C++异常处理assert,throw,exit用法

    常见的几个小细节问题. assert应用: 在现实世界中,我们脑袋时刻都在判断对与错,对的事情我们会继续深入下去,而错的事情我们会马上停止,那么在编程开发中我们如何赋予程序这种判断事物对错的能力呢?其 ...

  6. assert.fail()

    assert.fail(message) assert.fail(actual, expected[, message[, operator[, stackStartFunction]]]) oper ...

  7. assert.fail()详解

    assert.fail(actual, expected, message, operator) 抛出一个 AssertionError.如果 message 是假值,错误信息会被设置为被 opera ...

  8. 如何利用JUnit开展一个简单的单元测试(测试控制台输出是否正确)

    待测类(CreateString)如下: public class CreateString { public void createString() { //Output the following ...

  9. 利用VisualStudio单元测试框架举一个简单的单元测试例子

    本随笔很简单,不涉及mock和stub对象,而是只给出一个简单的利用Visual Studio单元测试框架的最简单例子.如果需要深入理解Unit Test的原理与艺术,请参考<The art o ...

随机推荐

  1. [RxJS] Stopping a Stream with TakeUntil

    Observables often need to be stopped before they are completed. This lesson shows how to use takeUnt ...

  2. [Javascript] Redirect the browser using JavaScript

    Three methods to preform redirection in browser: widnow.location.href window.location.assign window. ...

  3. Hexo博客搭建图文教程

    准备 你需要准备好以下软件: Node.js环境 Git Windows 配置Node.js环境 下载Node.js安装文件: Windows Installer 32-bit Windows Ins ...

  4. 批量SSH操作工具---OmniTTY安装

    安装rote # pwd /tmp/rote-0.2.8 # ./configure # make # make install ...... mkdir -p /usr/local/include/ ...

  5. Sybase安装后的配置工作

    1.配置数据库参数 配置sybase数据库使用的最大内存 用isql命令行实用工具登录sybase数据库服务器,其中的servername是$SYBASE/interfaces文件中配置的sybase ...

  6. iOS9TableView分割线默认不显示,只有滑动的时候才显示 解决办法

    只有iOS9和iPhone6 plus模拟器上TableView分割线不会显示,后来终于找到了原因: 由于iPhone6 plus的分辨率较高,开发的时候同常都使用command + 3 或者 com ...

  7. cell的各种使用和赋值 总结

    cell可以分为:自定义cell,系统的cell ,cell的自适应,.xib的cell //第一种cell:系统cell 在 UIViewController下创建UITableView //1.0 ...

  8. UI基础视图----UIScrollView总结

    UIScrollView是UIKit框架下的很重要的视图类,是UIView的子类.UILabel,UIImageView,UIWebView分别用于展示文字,图片,网页,UILabel通过属性text ...

  9. web worker使用

    使用postMessage()方法传递信息.来自Worker的数据保存在event.data中.通过message和error事件与页面通信. <script> var data = [4 ...

  10. 自动生成代码工具【JAVA版】

    发现任何项目无非五类操作:新增.修改.删除.查询详细.查询列表 大多数的服务端基础代码都是相同的,但是每次开发一个新项目都会做很多重复工作,从controller,bean,service,到数据库访 ...