Java 测试继承的构造函数时出现类型不匹配错误
我正在使用eclipse。当我尝试使用Junit测试一个类时,我在尝试创建一个对象时遇到了一个错误Java 测试继承的构造函数时出现类型不匹配错误,java,oop,abstract-class,subclass,Java,Oop,Abstract Class,Subclass,我正在使用eclipse。当我尝试使用Junit测试一个类时,我在尝试创建一个对象时遇到了一个错误 public abstract class Property { private int number = 0; private String street = null; private String city = null; private String postCode = null; private int numberOfRooms =
public abstract class Property {
private int number = 0;
private String street = null;
private String city = null;
private String postCode = null;
private int numberOfRooms = 0;
public Property(int number, String street, String city, String postCode, int numberOfRooms) {...}
...
}
这是junit测试文件:
public class PropertyTest {
@Test
public void testHouse() {
Property house = new House(1, "Percy Road", "London", "WC2N 5DU", 2);
assertEquals("1 Percy Road, London WC2N 5DU (2 bedroom house)", house.toString());
}
}
我在PropertyTest中遇到了这个错误:类型不匹配:无法将房屋转换为房产。
据我所知,此代码应该可以工作什么包是
属性
?你确定导入了正确的类吗?它们都在同一个包中。我刚刚删除了测试文件并从头创建了另一个,现在它可以工作了。我不知道为什么我以前没试过。ThanksSide注意:除非您真的想测试您的toString实现,否则请避免编写此类测试。如果要测试对象的内容,请使用getter。问题可能是导入错误的属性的import语句错误。
public class PropertyTest {
@Test
public void testHouse() {
Property house = new House(1, "Percy Road", "London", "WC2N 5DU", 2);
assertEquals("1 Percy Road, London WC2N 5DU (2 bedroom house)", house.toString());
}
}