我试图使用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());
}
}
答案 0 :(得分:3)
您应该覆盖equals
课程中的方法HotelRoom
。
@Override
public boolean equals(Object other){
//return true if memberwise equal
}
答案 1 :(得分:3)
你遗漏了一些非常基本的东西。您的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;
}
只有这样,您的酒店客房才会像您期望的那样进行比较。
请参阅: