Hallo Leute,
ich lerne gerade nach http://www.frankwestphal.de/UnitTestingmitJUnit.html Unit Testing. Hierzu habe ich folgende Klasse Euro:
und meine Testklasse:
Wieso funktioniert der testAdding nicht? Der Fehler lautet immer:
junit.framework.AssertionFailedError: sum expected:<3.0> but was:<300.0>
Könnt ihr mir weiterhelfen? Wäre dringend weil ich das wirklich nicht verstehe...
Vielen Dank,
Babsi
ich lerne gerade nach http://www.frankwestphal.de/UnitTestingmitJUnit.html Unit Testing. Hierzu habe ich folgende Klasse Euro:
Code:
public class Euro {
private long cents;
public Euro(double euro) {
if (euro < 0.0) {
throw new IllegalArgumentException("Negative amount");
}
cents = Math.round(euro * 100.0);
}
public double getAmount() {
return (cents / 100);
}
public int hashCode() {
return (int) cents;
}
public Euro add(Euro other) {
return new Euro(this.cents + other.cents);
}
public boolean equals(Object o) {
if (o == null || !o.getClass().equals(this.getClass())) {
return false;
}
Euro other = (Euro) o;
return this.cents == other.cents;
}
}
und meine Testklasse:
Code:
import junit.framework.*;
public class EuroTest extends TestCase {
private Euro two;
public EuroTest(String name) {
super(name);
}
protected void setUp() {
two = new Euro(2.00);
}
protected void tearDown() {
//two = null;
}
public void testAmount() {
assertTrue(2.00 == two.getAmount());
}
public void testRounding() {
Euro roundedTwo = new Euro(1.995);
assertEquals("rounded amount", 2.00, roundedTwo.getAmount(), 0.001);
}
public void testAdding() {
Euro three = two.add(new Euro(1.00));
assertEquals("two", 2.00, two.getAmount(), 0.001);
assertEquals("sum", 3.00, three.getAmount(), 0.001);
}
public void testEquality() {
assertEquals(two, two);
assertEquals(two, new Euro(2.00));
assertEquals(new Euro(2.00), two);
assertTrue(!two.equals(new Euro(0)));
assertTrue(!two.equals(null));
assertTrue(!two.equals(new Object()));
}
public void testHashCode() {
assertTrue(two.hashCode() == two.hashCode());
assertTrue(two.hashCode() == new Euro(2.00).hashCode());
}
public void testNegativeAmount() {
try {
final double NEGATIVE_AMOUNT = -2.00;
new Euro(NEGATIVE_AMOUNT);
fail("Should have raised an IllegalArgumentException");
}
catch (IllegalArgumentException expected) {
System.out.println("Exception works!");
}
}
public static void main(String[] args) {
junit.swingui.TestRunner.run(EuroTest.class);
}
}
Wieso funktioniert der testAdding nicht? Der Fehler lautet immer:
junit.framework.AssertionFailedError: sum expected:<3.0> but was:<300.0>
Könnt ihr mir weiterhelfen? Wäre dringend weil ich das wirklich nicht verstehe...
Vielen Dank,
Babsi