之前一篇博文(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. Android自定义控件(一)——开关控件

    Google 在 API 14 开始才新增了Switch 控件. 因此,我们可以选择自己封装一个Switch . 效果如图: View主要代码: public class SwitchView ext ...

  2. 利用JConsole工具监控java程序内存和JVM

    一.找到java应用程序对应的进程PI 性能测试应用程序访问地址:http://192.168.29.218:7070/training/ 部署的应用服务器为tomcat6.028 启动tomcat服 ...

  3. 解决 Google 重定向,体验 Google 本味

    想要体验原汁原味的 Google(google.com),下面的方案是我用过的较方便的方案. 欢迎更正及补充 Chrome 扩展 Chrone 商店有一款禁止重定向的扩展 NoCountryRedir ...

  4. 虚拟现实,增强现实,VR,AR

    现在的热点不止VR,还有AR和披着MR.HR.CR外衣的各种高级AR们,所以比较着一起说.以下知乎上一网友观点,放几条结论:1.近期(未来两三年)看,VR能火,AR尚待成熟: 2.VR设备中,插片式是 ...

  5. C#构造函数的 "继承" 问题

    首先说明下 之所以用 双引号 是因为构造函数是没有继承的 派生类默认会调用基类的无参数构造函数 比如: public class A         { public A()         { Co ...

  6. C# 模拟键盘按键操作

    [DllImport("user32.dll")] public static extern IntPtr keybd_event(byte bVk, byte bScan, in ...

  7. http status 源码

    private static readonly String[][] s_HTTPStatusDescriptions = new String[][] { null, new String[] { ...

  8. IEquatable(T) interface in .Net

    原文:http://weblogs.asp.net/pawanmishra/iequatable-t-interface-in-net 泛型方法: public static bool AreEqua ...

  9. Java中关于OOM的场景及解决方法

    原文地址:http://developer.51cto.com/art/201112/305696.htm 1.OOM for Heap=>例如:java.lang.OutOfMemoryErr ...

  10. 洛谷 P1241 括号序列

    P1241 括号序列 题目描述 定义如下规则序列(字符串): 1.空序列是规则序列: 2.如果S是规则序列,那么(S)和[S]也是规则序列: 3.如果A和B都是规则序列,那么AB也是规则序列. 例如, ...