gpt4 book ai didi

java - Junit 测试 - 没有我能看到的错误?

转载 作者:行者123 更新时间:2023-11-29 07:05:04 29 4
gpt4 key购买 nike

我试图对一种方法使用 Junit 测试,但测试失败了,尽管“预期”与“是……”相同。我想知道是什么问题?lowerRate 方法只是一种将“HotelRoom”作为输入并以 10% 折扣输出价格的方法。谢谢这是我的测试代码:

@Test
public void testLowerRate() {
System.out.println("lowerRate");
HotelRoom room = new HotelRoom(4, 300, "N");
HotelRoom instance = new HotelRoom(0, 0, "N");
HotelRoom expResult = new HotelRoom(4, 270, "N");
HotelRoom result = instance.lowerRate(room);
assertEquals(expResult, result);

}
@Test
@SuppressWarnings("empty-statement")
public void testLowerRate2() {
System.out.println("lowerRate");
HotelRoom room = new HotelRoom(4, 100, "N");
HotelRoom instance = new HotelRoom(0, 0, "N");
HotelRoom expResult = new HotelRoom(4, 90, "N");
HotelRoom result = instance.lowerRate(room);
assertEquals(expResult, result);

}
@Test
public void testLowerRate3() {
System.out.println("lowerRate");
HotelRoom room = new HotelRoom(4, 1000, "N");
HotelRoom instance = new HotelRoom(0, 0, "N");
HotelRoom expResult = new HotelRoom(4, 900, "N");
HotelRoom result = instance.lowerRate(room);
assertEquals(expResult, result);

}
}

编辑 ------

程序代码如下:

class HotelRoom {

private int numofBeds;
private double rateperNight;
private String YorNoceanView;

HotelRoom(int input1, double input2, String input3) {
numofBeds = input1;
rateperNight = input2;
YorNoceanView = input3;

}

public int getnumofBeds() {
return numofBeds;
}

public void setnumofBeds(int numofBeds) {
this.numofBeds = numofBeds;
}

public double getrateperNight() {
return rateperNight;
}

public void setrateperNight(double rate) {
this.rateperNight = rateperNight;
}

public String getYorNoceanView() {
return YorNoceanView;
}

public void setYorNoceanView(String YorNoceanView) {
this.YorNoceanView = YorNoceanView;
}

@Override
public String toString() {
return "Hotel Room{" + "Title= " + numofBeds + ", Rate per night= $" + rateperNight + ", Ocean View (Y or N)=" + YorNoceanView + "}";

}

String roomUnder(HotelRoom room, double price) {
if (room.getrateperNight() > price) {
return "The room costs more than the given price";
}
if (room.getrateperNight() < price) {
return "The room costs less than the given price";
}
else
return "The room costs as much as the given price.";
}
HotelRoom lowerRate(HotelRoom room) {
room.rateperNight = 0.9 * room.rateperNight;
return room;
}
}

public class Chpt2930HotelRoom {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

int num1 = 1;
int num2 = 2;
String input = "input";

Scanner scan = new Scanner(System.in);
Scanner user = new Scanner(System.in);
System.out.print("Enter number of Beds: ");
int nextLineBeds = scan.nextInt();
HotelRoom HotelR = new HotelRoom(num1, num2, input);
HotelR.setnumofBeds(nextLineBeds);
System.out.println();


System.out.print("Enter rate per night in Dollars: $");
double nextLineRate = scan.nextDouble();
HotelR.setrateperNight(nextLineRate);
System.out.println();


System.out.print("Enter whether the room has an ocean view (Y or N): ");
String nextLineOcean = user.nextLine();
HotelR.setYorNoceanView(nextLineOcean);
System.out.println();

System.out.println(HotelR.toString());
}
}

最佳答案

您缺少一些非常基本的东西。您的 lowerRate 方法返回一个 HotelRoom 类的对象,您正在使用 assertEquals 方法将该对象等同于一个完全不同的对象实例。这必然使对象引用相等(因为您没有覆盖 equals() 方法)。由于是两个不同的对象,它们的引用也不同,因此您的断言失败。

因为你想根据你的 rate 属性来均衡对象,那么你需要像这样重写 HotelRoom 类的 equals 方法:

@Override
public boolean equals(Object obj){

if(obj instanceof HotelRoom) {
if( Math.abs(this.rateperNight - ((HotelRoom) obj).rateperNight) < .000001) {
return true;
}
}
return false;
}

只有这样,您的酒店房间才会与您的期望进行比较。

参见:

关于java - Junit 测试 - 没有我能看到的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20708559/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com