JUnit代码覆盖率未完全涵盖

时间:2019-10-31 09:43:36

标签: junit

我为下面的类编写了测试用例,但问题仍然是它没有被100%覆盖,在我的下面的类中我没有提及>符号而不覆盖代码,有人可以建议我对剩余的代码该怎么做

存储库类

    public RowMapperServerResponse insertproductDetails(RowMapperServerResponse rowMapperServerResponse) {
            try {
                int insertRow = jdbcTemplate.update(
                        "insert into Master_Circuit (id_type,product,seq_no,name,dependent_count,create_time) values(?,?,?,?,?,?)",
                        rowMapperServerResponse.getId_type(), rowMapperServerResponse.getProduct(),
                        rowMapperServerResponse.getSeq_no(), rowMapperServerResponse.getName(),
                        rowMapperServerResponse.getDependent_count(), rowMapperServerResponse.getCreate_time());

        *
> if (insertRow != 0) {
>                       return rowMapperServerResponse;
>                   }
>               } catch (EmptyResultDataAccessException e) {
>                   return null;
>               }

*
            return null;
        }

测试案例

@Test
    public void insertproductDetailsTest() {

        baaisnEvcIdMSRepository.insertproductDetails(rowResponse());

        Mockito.when(jdbcTemplate.update(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
                Mockito.anyInt(), Mockito.anyString(), Mockito.anyInt(), Mockito.any())).thenReturn(1);

        Mockito.verify(jdbcTemplate, Mockito.times(1)).update(Mockito.anyString(), Mockito.anyString(),
                Mockito.anyString(), Mockito.anyInt(), Mockito.anyString(), Mockito.anyInt(), Mockito.any());
    }

    @Test
    public void insertproductDetailsEmptyTest() {

        baaisnEvcIdMSRepository.insertproductDetails(rowResponse());

        Mockito.when(jdbcTemplate.update(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
                Mockito.anyInt(), Mockito.anyString(), Mockito.anyInt(), Mockito.any())).thenReturn(0);

        Mockito.verify(jdbcTemplate, Mockito.times(1)).update(Mockito.anyString(), Mockito.anyString(),
                Mockito.anyString(), Mockito.anyInt(), Mockito.anyString(), Mockito.anyInt(), Mockito.any());
    }

    private RowMapperServerResponse rowResponse() {

        RowMapperServerResponse rowMapperServerResponse = mock(RowMapperServerResponse.class);
        Mockito.when(rowMapperServerResponse.getMaster_kit_id()).thenReturn(464662);
        Mockito.when(rowMapperServerResponse.getId_type()).thenReturn("EMSsample");
        Mockito.when(rowMapperServerResponse.getProduct()).thenReturn("sample");
        Mockito.when(rowMapperServerResponse.getSeq_no()).thenReturn(36316);
        Mockito.when(rowMapperServerResponse.getName()).thenReturn("TLS");
        Mockito.when(rowMapperServerResponse.getDependent_count()).thenReturn(0);
        Mockito.when(rowMapperServerResponse.getCreate_time()).thenReturn(new Date());

        return rowMapperServerResponse;
    }

1 个答案:

答案 0 :(得分:0)

我建议仅在if块中放置一个断点。 如果没有覆盖代码,则显然没有测试进入该块。

一个可能的问题是您调用baaisnEvcIdMSRepository.insertproductDetails,然后才在测试中的下一行指定Mockito中的期望值。我不是模拟专家,但是在“ Subject Under Test”执行期间,Mockito如何知道您期望它返回0/1?如果使用某些默认值,则它始终可以返回0,因此如果不会执行分支。