我正在大学开展一个项目,我将创建自己的对象,以及私有数据,方法等。这不是一个带有用户界面的完整工作系统;它只是创建类的机会,然后实例化并测试它们。
问题是我正在尝试创建一个测试方法,该方法在名为DinetteStore的构造函数中测试抛出的IllegalArgumentException:
public DinetteStore(int tableInventory, int chairInventory, int leafInventory){
if (tableInventory < 0 || leafInventory < 0 || chairInventory < 0){
throw new IllegalArgumentException ("Inventory must not be out of range of required order");
}
this.tableInventory = tableInventory;
this.chairInventory = chairInventory;
this.leafInventory = leafInventory;
this.totalSales = 0;
this.numSales = 0;
}
此处列出了测试类中的代码:
@Test (expected = IllegalArgumentException.class)
public void testIllegalArgumentChair() {
int tableInventory = -1 || int leafInventory = -1 || chairInventory = -1;
}
我遇到了一个问题,我遇到了.class预期错误或非法启动表达错误。我正在使用的IDE是BlueJ 4.1.0这里有什么我缺少语法的吗?任何帮助肯定会受到赞赏。
答案 0 :(得分:3)
您没有实例化class
会引发exception
。你的语法不正确,它应该是这样的:
@Test (expected = IllegalArgumentException.class)
public void testIllegalArgumentChair() {
DinetteStore willFail = new DinetteStore(-1, -1, -1);
}
答案 1 :(得分:2)
@Test (expected = IllegalArgumentException.class)
public void testIllegalArgumentChair() {
DinetteStore d = new DinetteStore(-1,-1,-1);
}
在测试中你必须调用抛出异常的方法,否则你永远不会得到预期的异常
答案 2 :(得分:2)
您的测试类格式很奇怪。您正尝试使用非法参数实例化DinetteStore
,因此您需要:
@Test (expected = IllegalArgumentException.class)
public void testIllegalArgumentChair() {
int tableInventory = -1;
int leafInventory = -1;
int chairInventory = -1;
DinetteStore creationWillFail = new DinetteStore(tableInventory,
leafInventory,
chairInventory);
}