zoukankan      html  css  js  c++  java
  • JUnit实战(1)

    创建Java Project项目,项目名称:ch01-jumpstart

    Calculator.java

    public class Calculator {
        public double add(double number1, double number2) {
            return number1 + number2;
        }
    }

    CalculatorTest.java

    import static org.junit.Assert.*;
    import junit.framework.Assert;
    
    import org.junit.Test;
    
    public class CalculatorTest {
        @Test
        public void testAdd() {
            Calculator calculator = new Calculator();
            double result = calculator.add(10, 50);
            assertEquals(60, result, 0);
        }
    }

    注:使用静态导入可以使被导入类的静态变量和静态方法在当前类直接可见,使用这些静态成员无需再给出他们的类名(如assertEquals(double expected, double actual, double delta))。

    ParameteredCalculatorTest.java

    import static org.junit.Assert.assertEquals;
    
    import java.util.Arrays;
    import java.util.Collection;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized;
    import org.junit.runners.Parameterized.Parameters;
    
    @RunWith(value=Parameterized.class)
    public class ParameterizedCalculatorTest {
    
        private double expected; 
        private double valueOne; 
        private double valueTwo; 
    
        @Parameters 
        public static Collection<Integer[]> getTestParameters() {
           return Arrays.asList(new Integer[][] {
              {2, 1, 1},  //expected, valueOne, valueTwo   
              {3, 2, 1},  //expected, valueOne, valueTwo   
              {4, 3, 1},  //expected, valueOne, valueTwo   
           });
        }
    
        public ParameterizedCalculatorTest(double expected, 
           double valueOne, double valueTwo) {
           this.expected = expected;
           this.valueOne = valueOne;
           this.valueTwo = valueTwo;
        }
    
        @Test
        public void sum() {
           Calculator calc = new Calculator();
           assertEquals(expected, calc.add(valueOne, valueTwo), 0);
        } 
    }

    注:通过注解运行参数化测试

  • 相关阅读:
    Centos 下oracle 11g 安装部署及手动建库过程
    MongoDB 存储引擎Wiredtiger原理剖析
    有关RDS上只读实例延时分析-同适用于自建MySQL主从延时分析判断
    windows 下my.ini的配置优化
    什么是purge操作
    linux内核调优参考
    通过第三方镜像仓库代理下载镜像
    微积分拾遗——链式法则
    Java中的RASP实现
    机器学习是什么
  • 原文地址:https://www.cnblogs.com/thlzhf/p/4276005.html
Copyright © 2011-2022 走看看