zoukankan      html  css  js  c++  java
  • 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());// 抛出的异常信息是否和期望一致
            
            //如果上面两个都通过,则测试通过
    
        }
    
    }
  • 相关阅读:
    Python中最常见的10个列表操作
    使用Mac的texturetool将图片转换为PVRTC格式
    Android、iOS复制到粘贴板
    Python
    Pytest 作业
    Jmeter系列(34)- 详解 Jmeter CLI 模式
    Jmeter系列(33)- Jmeter 分布式测试
    Jmeter系列(32)- 详解性能监控工具 nmon
    Jmeter系列(31)- 详解 ForEach控制器
    Jmeter系列(30)- 详解 Loop Controller 循环控制器
  • 原文地址:https://www.cnblogs.com/huangcongcong/p/4749637.html
Copyright © 2011-2022 走看看