Reputation: 3
I am getting initializatoinError.
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);
}
}
Any help would be appreciated.
Upvotes: 0
Views: 3855
Reputation: 415
The error says exactly what is wrong. Your class doesn't have "exactly one public zero-argument constructor".
But the gold rule is to have tests outside of business classes. So create new class called public class ComplexTest
and put your test method there.
Upvotes: 1