为什么测试覆盖范围显示红色

时间:2020-03-21 04:51:04

标签: spring-boot mockito

此功能用于查询数据库以搜索特定类别。我为此功能编写的测试用例覆盖了整个代码,当我使用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;
}

enter image description here

1 个答案:

答案 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方法的逻辑,因为它一旦执行就会引发错误