i want to add an object in DB than check if the object is already there so we can't add it twice. I used JUNIT to test it:
@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
}
The problem is that i can't delete the product since the instruction that's called before will throw a exception thus end the test.
好吧,我有点弄清楚了...我调用@Before方法witch从BD删除所有文件并放回默认值
Maybe something like this solving this problem. More is on https://stackoverflow.com/a/20494165/4296891 by Sergey Berezovskiy
public void AddExisting()
{
Register aRegister = new Register();
Product aProduct = new Product();
aProduct.setPIN("079400027252");
aRegister.AddProduct(aProduct);
Product sameProduct = new Product();
sameProduct.setPIN("079400027252");
try
{
aRegister.AddProduct(sameProduct); //this throw the exception
assertEquals("Can't add it twice exception has been excepted." ,true, false);
}
catch(ExistingProduct exception)
{
// exception
}
aRegister.deleteProduct("079400027252"); //CAN'T REACH HERE
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.