[英]JUNIT Test case not passed
I have class where I am written code to create a new category and if category not found it will throw "CategoryNotFoundException". 我在编写代码的类中创建了一个新类别,如果找不到该类别,它将抛出“ CategoryNotFoundException”。 I have written the test cases but it was not passed.
我已经写了测试用例,但是没有通过。
If I omit "expected= CategoryNotFoundException.class" from my JUNIT test case it will pass. 如果我在JUNIT测试用例中省略了“ expected = CategoryNotFoundException.class”,它将通过。 But I don't want to change any portion in my Test cases.
但是我不想更改测试用例中的任何部分。 I tried by throwing the exception from my implemented class but still it is not passed.I am stucking there to pass the TestCase.
我试图通过从实现的类中抛出异常来尝试,但仍然没有通过,我坚持在那里通过了TestCase。
DAO code::
@Transactional
public boolean createCategory(Category category){
//boolean isInserted=false;
Session session=this.sessionFactory.getCurrentSession();
session.save(category);
return true;
//return isInserted;
}
Tried with the below code as well but TC not passed: 也尝试使用以下代码,但未通过TC:
@Transactional
public boolean createCategory(Category category){
//boolean isInserted=false;
try{
Session session=this.sessionFactory.getCurrentSession();
Integer isInsertedWrapper=(Integer)session.save(category);
if(isInsertedWrapper>0){
return true;
}else{
throw new CategoryNotFoundException("CategoryNotFoundException");
}
}
catch(Exception ex){
return false;
}
}
JUNIT Code:: JUNIT代码::
@Test(expected= CategoryNotFoundException.class)
@Rollback(true)
public void testCreateCategoryFailure() throws CategoryNotFoundException {
categoryDAO.createCategory(category);
Category savedCategory = categoryDAO.getCategoryById(2);
assertNotEquals(category, savedCategory);`enter code here`
}
You are trying to throw an exception but you are catching also, so you should rethrow If it is necessary, so you should try to do this: 您正在尝试引发异常,但同时也正在捕捉,因此如果有必要,您应该重新抛出异常,因此您应该尝试执行以下操作:
@Transactional
public boolean createCategory(Category category){
//boolean isInserted=false;
try {
Session session=this.sessionFactory.getCurrentSession();
Integer isInsertedWrapper=(Integer)session.save(category);
if(isInsertedWrapper>0){
return true;
}else{
throw new CategoryNotFoundException("CategoryNotFoundException");
}
} catch(CategoryNotFoundException exc) {
throw exc;
} catch(Exception ex){
return false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.