zoukankan      html  css  js  c++  java
  • 软件测试 —— 如何测试私有构造函数(反射机制)

    要测试的类:

    public class Argument {
    
        private final Value value;
        private final Variable variable;
        private final boolean isValue;
    
        private Argument(Value value, Variable variable, boolean isValue) {
            this.value = value;
            this.variable = variable;
            this.isValue = isValue;
        }
    
        public static Argument value(Value value) {
            return new Argument(value, null, true);
        }
    
        public static Argument variable(Variable variable) {
            return new Argument(null, variable, false);
        }
    
        public boolean isValue() {
            return this.isValue;
        }
    
        public boolean isVariable() {
            return !this.isValue;
        }
    
        public Value getValue() {
            return this.value;
        }
    
        public Variable getVariable() {
            return this.variable;
        }
    
        public String toString() {
            if (isValue)
                return value.toString();
            return variable.toString();
        }
    
    }

    测试用例(仅写一个测试)

      @Test
        public void testIsValue() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            Value v1 = new Value("Test01");
            Variable v2 = new Variable("Test02");
            
            Class clazz = Argument.class;
            //调用带参的、私有构造函数
            Constructor c1 = clazz.getDeclaredConstructor(new Class[]{Value.class, Variable.class, boolean.class});
            c1.setAccessible(true);    
            Argument t = (Argument) c1.newInstance(new Object[]{v1, v2, true});
    t.value(v1); assertEquals(
    true, t.isValue()); t.variable(v2); assertEquals(true, t.isValue()); }
  • 相关阅读:
    html5+css3实现上拉和下拉刷新
    js求时间差
    screenX clientX pageX的区别
    HTML5实战与剖析之触摸事件(touchstart、touchmove和touchend)
    国内代码托管git-osc基础使用教程
    c# 实现获取汉字十六进制Unicode编码字符串
    C# 判断字符编码的六种方法
    UNICODE 区域对照表
    viewport
    Wingdings 2 符号编码对照表
  • 原文地址:https://www.cnblogs.com/douzujun/p/7577174.html
Copyright © 2011-2022 走看看