我在产品实现中有以下代码
public void updateProduct(Product product) throws ProductNotFoundException {
Iterator<Product> it = allProducts.iterator();
Product p = null;
if (!allProducts.isEmpty()) {
while (it.hasNext()) {
p = it.next();
if (p.getId() == product.getId()) {
p.setPrice(product.getPrice());
System.out.println("Successfully updated the product "
+ product.getId());
}
}
} else {
System.out.println("No such product " + product.getId());
throw new ProductNotFoundException(product.getName());
}
}
// 主要的
ipod.setId(Integer.MAX_VALUE);
try {
productDB.updateProduct(ipod);
Assert.fail("should've gotten ProductNotFoundException");
}
catch (ProductNotFoundException pnfe) {
// expecting this
}
Junit 抛出以下异常
Exception in thread "main" productdb.util.AssertionFailedError: should've gotten ProductNotFoundException
at productdb.util.Assert.fail(Assert.java:43)
**at productdb.ProductDBClient.testProductServer(ProductDBClient.java:85)**
at productdb.ProductDBClient.main(ProductDBClient.java:20)
不幸的是,我无法使用 try and catch。编译器抛出错误,说它无法被捕获。
你能给我一些我做错的地方吗?
非常感谢!!