JUnit assertTrue对于两个字符串是不明确的,而对于doubles,测试用例失败



我正在尝试编写一个JUnit测试,以确认我的变量保存正确。我很难通过双打和字符串的测试用例

@Test
public void testConstructor() {
Flight test = new Flight(200, "Canada", "Mexico", "03/02/99 7:50 pm", 500, 10); 
assertEquals(200, test.flightNumber);
assertEquals(10, test.capacity);
assertEquals(Double.valueOf(500), test.originalPrice);

String a ="Canada";
String b = test.origin;
assertTrue(a.equals(b));
}

尽管两个值显然都应该是500,但我的双重情况似乎失败了,我得到了assertTrue:的错误

Assert中的方法assertTrue(布尔值(和方法断言中的assertTrue(布尔值(与匹配

我甚至不知道为什么它说布尔值,它们不应该是字符串吗?

这是测试用例正在测试的代码:

public int flightNumber, capacity, numberOfSeatsLeft;
public double originalPrice;
public String origin, destination, departureTime;
public Flight(int flightNumber, String origin, String destination, String departureTime, double originalPrice, int capacity) {
if (destination.equals(origin)) {
throw new IllegalArgumentException("Already at destination");
}
this.origin=origin;
this.destination=destination;
this.departureTime=departureTime;
this.originalPrice=originalPrice;
this.flightNumber=flightNumber;
this.capacity=capacity;
}

您正在尝试使用assertEquals(Double, double),一个包装类及其对应的原语,而junit中没有这样的方法。这是因为字段originalPricedouble,但Double.valueOf(500)返回一个Double。您应该使用assertEquals(double, double)(但已弃用(或assertEquals(Object, Object)。你可以这样称呼它:

assertEquals(Double.valueOf(500), Double.valueOf(test.originalPrice));

最新更新