5.2

封装类:

package com.szys.junit;


public class T {
    int a;
    int b;
    public T(int a,int b)throws Exception
    {
        this.a=a;
        this.b=b;
        if(a<0||a>100||b<0||b>100)
            throw new Exception("数字超出范围"); 
    }
    
    
    public int add()
    {
        return a + b;
    }
    public int minus()
    {
        return a - b;
    }
    public int mul()
    {
        return a * b;
    }
    public int div()throws Exception
    {
        if(0 == b)
        {
            throw new Exception("除数不能为0");            
        }
        return a / b;

    }
    
}
T

测试类:

package com.szys.junit.test;

import static org.junit.Assert.*;

import org.junit.Test;

import com.szys.junit.T;

public class TTest {

    @Test
    public void test() {
        try {
        int a=new T(1, 2).add();
        assertEquals(3, a);
        System.out.println(a);
        int b=new T(4, 3).minus();
        assertEquals(1,b);
        System.out.println(b);
        int c=new T(3, 4).mul();
        assertEquals(12,c);
        System.out.println(c);
        int d=new T(4, 4).div();
        System.out.println(d);
        assertEquals(1, d);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    

}
TTest

测试用例:

用例 测试数据 预期输出 实际输出 测试状态
1 T(1, 2).add(); 3 3 答对了
2 int b=new T(4, 3).minus(); 1 1 答对了
3 int c=new T(3, 4).mul(); 12 12 答对了
4 int d=new T(4, 4).div(); 1 1 答对了
5 T(1, 101).add(); 102 异常:数字超出范围
6 int d=new T(4, 0).div(); 异常:除数不能为0

   总结:

        这次只是有上次的基础上增加了数字超出范围的异常和测试用例,当时那个测试代码的覆盖率不明白是什么意思。

原文地址:https://www.cnblogs.com/wuzijian/p/4486148.html