initializationError: Test class should have exactly one public zero-argument

initializationError: Test class should have exactly one public zero-argument

我收到初始化错误。

java.lang.Exception: Test class should have exactly one public zero-argument constructor My code is (This is an example from Java Programming Interview Exposed):

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);
    }
}

如有任何帮助,我们将不胜感激。

该错误准确说明了错误所在。你的 class 没有 "exactly one public zero-argument constructor"。

但黄金法则是在业务之外进行测试 classes。因此,创建名为 public class ComplexTest 的新 class 并将您的测试方法放在那里。