我想在数据库中添加一个对象而不是检查该对象是否已经存在所以我们不能添加它两次。我用 JUNIT 来测试它:
@org.junit.Test (expected = ExistingProduct.class)
public void AddExisting()
{
Register aRegister = new Register();
Product aProduct = new Product();
aProduct.setPIN("079400027252");
aRegister.AddProduct(aProduct);
Product sameProduct = new Product();
sameProduct.setPIN("079400027252");
aRegister.AddProduct(sameProduct); //this throw the exception
aRegister.deleteProduct("079400027252"); //CAN'T REACH HERE
}
问题是我无法删除产品,因为之前调用的指令会引发异常从而结束测试。