此功能用于查询数据库以搜索特定类别。我为此功能编写的测试用例覆盖了整个代码,当我使用eclipse ecelma看到代码覆盖率时,它在特定行上显示红色。有人可以帮我纠正这个问题吗?
@Override
public List<Services> searchCategory(String name) throws CategoryNameNotFoundException{
logger.info("{}.{}",new ServicesBoImpl().getClass().getPackageName(), new ServicesBoImpl().getClass().getName());
logger.info("Function: searchCategory(), Information: querying the database for the search categories");
List<Services> searchCategory = jdbcTemplate.query(env.getProperty("searchCategory"), new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, name+"%");
}
} ,new SearchCategoryRowMapper());
if(searchCategory.size()==0) {
logger.info("Function: searchCategory(), Information: Throwing CategoryNameNotFoundException because the paticular category is not found");
throw new CategoryNameNotFoundException("Category Not Found");
}
return searchCategory;
}
答案 0 :(得分:0)
您打算通过编写此UT涵盖哪些逻辑?测试内部没有执行任何逻辑...
假设serviceBoImpl
是模拟的:
@Test(expected = CategoryNameNotFoundException.class)
public void testIfSearchCategoryThrowsException() {
Mockito.doThrow(CategoryNameNotFoundException).when(serviceBoImpl).searchCategory("a!@")
...here execution which calls this method...
}
请记住,此测试不会涵盖searchCategory
方法的逻辑,因为它一旦执行就会引发错误