JUnit3 结合一个除法的单元测试说明Assert.fail()的用法
之前一篇博文(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()的用法的更多相关文章
- 单元测试中Assert类的用法
Assert类所在的命名空间为Microsoft.VisualStudio.TestTools.UnitTesting 在工程文件中只要引用Microsoft.VisualStudio.Quality ...
- VS单元测试中Assert类的用法
首先说介绍一下,Assert类所在的命名空间为Microsoft.VisualStudio.TestTools.UnitTesting 在工程文件中只要引用Microsoft.VisualStudio ...
- 单元测试中Assert类
一.Assert类的使用 1.Assert类所在的命名空间为Microsoft.VisualStudio.TestTools.UnitTesting 在工程文件中只要引用Microsoft.Visua ...
- 【php】assert函数的用法
[php]assert函数的用法 http://www.douban.com/note/217557007/ 2012-06-01 10:32:37 assert这个函数在php语言中是用来判断一 ...
- C++异常处理assert,throw,exit用法
常见的几个小细节问题. assert应用: 在现实世界中,我们脑袋时刻都在判断对与错,对的事情我们会继续深入下去,而错的事情我们会马上停止,那么在编程开发中我们如何赋予程序这种判断事物对错的能力呢?其 ...
- assert.fail()
assert.fail(message) assert.fail(actual, expected[, message[, operator[, stackStartFunction]]]) oper ...
- assert.fail()详解
assert.fail(actual, expected, message, operator) 抛出一个 AssertionError.如果 message 是假值,错误信息会被设置为被 opera ...
- 如何利用JUnit开展一个简单的单元测试(测试控制台输出是否正确)
待测类(CreateString)如下: public class CreateString { public void createString() { //Output the following ...
- 利用VisualStudio单元测试框架举一个简单的单元测试例子
本随笔很简单,不涉及mock和stub对象,而是只给出一个简单的利用Visual Studio单元测试框架的最简单例子.如果需要深入理解Unit Test的原理与艺术,请参考<The art o ...
随机推荐
- PyInstaller打包Python脚本为exe
1.PyInstaller-3.1.1 百度云链接 http://pan.baidu.com/s/1jHYWin8 密码 oapl 2.安装最新版本的 pywin32-217.win32-py2 ...
- FileUtil.java
package com.founder.util.file; import java.io.BufferedReader; import java.io.File; import java.io.Fi ...
- [Redux] Passing the Store Down with <Provider> from React Redux
Previously, we wrote the Provider component by ourself: class Provider extends Component { getChildC ...
- 通过JS控制textarea的输入长度
废话不多说,直接上代码(因为自己也只是遇到的时候然后上网查到的解决办法,放在此处只是为了方便各位看以及以后再碰到用起来方便) <ul> <li> <textarea ro ...
- Java性能漫谈-数组复制之System.arraycopy
当我还年幼的时候,我很任性,复制数组也是,写一个for循环,来回倒腾,后来长大了,就发现了System.arraycopy的好处. 为了测试俩者的区别我写了一个简单赋值int[100000]的程序来对 ...
- .net 实现注册邮箱验证激活
没事上网当了个注册邮箱验证激活的代码,用起来感觉还不错,特意和大家要一起分享一下 下面是主要实现代码: uing System.Net.Mail; public partial class jquer ...
- (二)backbone - DEMO - user list
Demo介绍 学习了如何基本的使用Backbone,展示用户信息 使用JQuery操作DOM,backbone.localStorage.js操作localstorage 大体实现 •创建user M ...
- Jquery去除从数据库中查询到的内容含有的p标签
$("#topic_content").html($("#topic_content").text()); 如果这个数据是通过循环遍历出的数据,就需要下面这个代 ...
- 【Nutch基础教程之七】Nutch的2种运行模式:local及deploy
在对nutch源代码运行ant runtime后,会创建一个runtime的目录,在runtime目录下有deploy和local 2个目录. [jediael@jediael runtime]$ l ...
- 使用 PHP 读取文本(TXT)文件 并分页显示
<?php //----------------you should save this file as m.php---------------- session_start(); if (e ...