之前一篇博文(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. Map的内容按字母顺序排序

    map有自带的排序功能,但需要重写排序方法,代码如下: package coreJava.com.shindo.corejava.map; import java.util.ArrayList; im ...

  2. javascript定时器:setTimeout与setInterval

    概述: setTimeout:在指定的延迟时间之后调用一个函数或者执行一个代码片段,只执行一次: setInterval:周期性地调用一个函数(function)或者执行一段代码,重复执行: 语法格式 ...

  3. JMeter简单的性能测试实例

    JMeter基础之——一个简单的性能测试 我们了解了jmeter的一此主要元件,那么这些元件如何使用到性能测试中呢.这一节创建一个简单的测试计划来使用这些元件.该计划对应的测试需求. 1)测试目标网站 ...

  4. [Redux] Generating Containers with connect() from React Redux (VisibleTodoList)

    Learn how to use the that comes with React Redux instead of the hand-rolled implementation from the ...

  5. 关于时间的操作(JavaScript版)——年月日三级级联(默认依次显示请选择年、请选择月和请选择日)

    这篇博客和前一篇博客基本同样,仅仅是显示的默认值不同: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN&quo ...

  6. AngularJs练习Demo2

    @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...

  7. ios9API基础知识总结(二)

    UIAlertView(警告框) UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"标题" message:@&qu ...

  8. Light oj 1030 二分查找

    1088 - Points in Segments   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...

  9. Render和template?

    Template是一个模板. render = web.template.render('templates/') 这会告诉web.py到你的模板目录中去查找模板.然后把 index.GET改成: 告 ...

  10. python 杂记

    class TestA(object): def __init__(self): print("A is initing"); def foo(self): print(" ...