zoukankan      html  css  js  c++  java
  • Myeclipse 使用JUnit 进行单元测试

    Java计算器源码(只实现了加减乘除四种简单的运算):

     1 import java.math.BigDecimal;
     2 import java.text.Format;
     3 public class Caculator {
     4 private BigDecimal num1;
     5 private BigDecimal num2;
     6 private String result;
     7 
     8 public void setNum1(double num1) {
     9     this.num1 = new BigDecimal(num1);
    10 }
    11 
    12 public void setNum2(double num2) {
    13     this.num2 = new BigDecimal(num2);
    14 }
    15 private double _format(){
    16     BigDecimal _num1 = new BigDecimal(result);
    17     BigDecimal _num2 = new BigDecimal(1);
    18     return _num1.divide(_num2, 5,BigDecimal.ROUND_HALF_UP).doubleValue();    
    19 }
    20 public String getResult() {
    21     if(!result.equals("ERROR"))
    22     {
    23         double _res = _format();
    24         if(_res>=Double.MAX_VALUE||_res<=-Double.MAX_VALUE)
    25             result = "ERROR";
    26         else
    27             result = Double.toString(_format());        
    28     }        
    29     return result;
    30 }
    31 public void Add() {
    32     BigDecimal _result;
    33     _result = num1.add(num2);
    34     result = _result.toString();    
    35 }
    36 public void Sub() {
    37     BigDecimal _result;
    38     _result = num1.subtract(num2);
    39     result = _result.toString();    
    40 }
    41 public void Mul() {
    42     BigDecimal _result;
    43     _result = num1.multiply(num2);
    44     result = _result.toString();    
    45 }
    46 public void Div() {
    47     BigDecimal _result;
    48     if(num2.doubleValue()!=0){
    49         _result = num1.divide(num2);
    50         result = _result.toString();
    51     }
    52     else 
    53         result = "ERROR";
    54     }
    55     }

    设计测试用例:

    JUnit测试用例代码:

     1 import org.junit.Test;
     2 import java.math.BigDecimal;
     3 import junit.framework.TestCase;
     4 public class caculate_test extends TestCase {
     5     private Caculator ca = new Caculator();
     6     protected void setUp() throws Exception {
     7         super.setUp();
     8     }
     9     @Test
    10     public void test_Add() {
    11         ca.setNum1(1);
    12         ca.setNum2(2);
    13         ca.Add();
    14         assertEquals("3.0", ca.getResult());
    15         ca.setNum1(2.4);
    16         ca.setNum2(1.2);
    17         ca.Add();
    18         assertEquals("3.6", ca.getResult());
    19         ca.setNum1(Double.MAX_VALUE);
    20         ca.setNum2(2);
    21         ca.Add();
    22         assertEquals("ERROR", ca.getResult());
    23         }
    24     @Test
    25     public void test_Sub() {
    26         ca.setNum1(1);
    27         ca.setNum2(2);
    28         ca.Sub();
    29         assertEquals("-1.0", ca.getResult());
    30         ca.setNum1(2.4);
    31         ca.setNum2(1.2);
    32         ca.Sub();
    33         assertEquals("1.2", ca.getResult());
    34         ca.setNum1(-Double.MAX_VALUE);
    35         ca.setNum2(2);
    36         ca.Sub();
    37         assertEquals("ERROR", ca.getResult());
    38         }
    39     @Test
    40     public void test_Mul() {
    41         ca.setNum1(1);
    42         ca.setNum2(2);
    43         ca.Mul();
    44         assertEquals("2.0", ca.getResult());
    45         ca.setNum1(2.4);
    46         ca.setNum2(1.2);
    47         ca.Mul();
    48         assertEquals("2.88", ca.getResult());
    49         ca.setNum1(1.3);
    50         ca.setNum2(0);
    51         ca.Mul();
    52         assertEquals("0.0", ca.getResult());
    53         ca.setNum1(Double.MAX_VALUE);
    54         ca.setNum2(2);
    55         ca.Mul();
    56         assertEquals("ERROR", ca.getResult());
    57         }
    58     @Test
    59     public void test_Div() {
    60         ca.setNum1(1);
    61         ca.setNum2(2);
    62         ca.Div();
    63         assertEquals("0.5", ca.getResult());
    64         ca.setNum1(2.4);
    65         ca.setNum2(1.2);
    66         ca.Div();
    67         assertEquals("2.0", ca.getResult());
    68         ca.setNum1(1.3);
    69         ca.setNum2(0);
    70         ca.Div();
    71         assertEquals("ERROR", ca.getResult());
    72         }
    73 }

    测试结果:

    GitHub:https://github.com/TongWee/SoftwareTesting_Caculator/

  • 相关阅读:
    iOS开发时区缩写
    HTTPS工作原理-默写
    【Swift学习笔记-《PRODUCT》读书记录-CoreData数据存储】
    判断是否是满二叉树
    词向量
    百面机器学习|第一章 特征工程
    c++中创建二维数组的几种方法
    深度学习入门|第七章 卷积神经网络(三)
    深度学习入门|第六章与学习相关的技巧(二)
    深度学习入门|第七章卷积神经网络
  • 原文地址:https://www.cnblogs.com/TongWee/p/4528266.html
Copyright © 2011-2022 走看看