initializationError:测试类应恰好具有一个公共零参数

问题描述 投票:0回答:1

我正在收到InitializatoinError。

java.lang.Exception:测试类应仅具有一个公共的零参数构造函数我的代码是(这是暴露于Java Programming Interview中的示例):

import org.junit.*;

import static org.junit.Assert.*;

public class Complex {
    private final double real;
    private final double imaginary;

    public Complex(final double r, final double i) {
        this.real = r;
        this.imaginary = i;
    }
    public Complex add(final Complex other) {
        return new Complex(this.real + other.real,
                this.imaginary + other.imaginary);
    }
    // hashCode omitted for brevity
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Complex complex = (Complex) o;
        if (Double.compare(complex.imaginary, imaginary) != 0) return false;
        if (Double.compare(complex.real, real) != 0) return false;
        return true;
    }

    @Test
    public void complexNumberAddition() {
        final Complex expected = new Complex(6,2);
        final Complex a = new Complex(8,0);
        final Complex b = new Complex(-2,2);
        assertEquals(a.add(b), expected);
    }
}

任何帮助将不胜感激。

java junit4
1个回答
0
投票

错误确切说明了错误所在。您的课程没有“完全是一个公共的零参数构造函数”。

但是黄金法则是在商务舱之外进行测试。因此,创建一个名为public class ComplexTest的新类,并将您的测试方法放在此处。

© www.soinside.com 2019 - 2024. All rights reserved.