gpt4 book ai didi

java - Jacoco 需要对 Lambdas Java8 进行特殊处理吗?

转载 作者:行者123 更新时间:2023-12-04 13:46:41 26 4
gpt4 key购买 nike

我读过几篇关于 Jacoco 支持 Lambda 函数的旧帖子,问题在几年前得到解决。
我发现当我运行 Jacoco 时,它没有报告此代码中 Lambda 函数的覆盖率

        List<SubmissionStatus> result = jdbcTemplate.query(
FINDALL_SQL,
(rs, rowNum) -> new SubmissionStatus(
rs.getLong("subm_rec_id"),
rs.getLong("subm_file_id"),
rs.getString("contract_id"),
rs.getString("contract_name"),
rs.getString("status"))
);
我知道它被击中是因为测试无法通过。
我是否需要为 Jacoco 做一些特别的事情才能正确报告覆盖范围?

最佳答案

您可以尝试这样的方法来获得 100% 的覆盖率。

在源代码上,你可以这样修改

List<SubmissionStatus> result = jdbcTemplate.query(
FINDALL_SQL, getSubmissionStatusRowMapper()
);

protected RowMapper<SubmissionStatus> getSubmissionStatusRowMapper() {
return (ResultSet row, int rowNum) -> new SubmissionStatus(
row.getLong("subm_rec_id"),
row.getLong("subm_file_id"),
row.getString("contract_id"),
row.getString("contract_name"),
row.getString("status")
);
}

您可以使用 Mockito 创建 Junit Test 类以使其工作
@RunWith(MockitoJUnitRunner.class)
public class SubmissionStatusDAOTest {
private static final long SUBM_REC_ID= 1;
private static final long SUBM_FILE_ID= 2;
private static final String CONTRACT_ID= "123";
private static final String CONTRACT_NAME= "ABC";
private static final String STATUS = "SUCCESS";

@InjectMocks
private SubmissionStatusDAO dao;

@Mock
private JdbcTemplate jdbcTemplate;

@Mock
private ResultSet resultSet;


@Before
public void prepareTest() throws SQLException {
when(resultSet.getLong("subm_rec_id")).thenReturn(SUBM_REC_ID);
when(resultSet.getLong("subm_file_id")).thenReturn(SUBM_FILE_ID);
when(resultSet.getString("contract_id")).thenReturn(CONTRACT_ID);
when(resultSet.getString("contract_name")).thenReturn(CONTRACT_NAME);
when(resultSet.getString("status")).thenReturn(STATUS);
}

@Test
public void test() throws SQLException {
RowMapper<SubmissionStatus> rowMapper = dao.getSubmissionStatusRowMapper();

SubmissionStatus submissionStatus = rowMapper.mapRow(resultSet, 1);
assertEquals(SUBM_REC_ID, submissionStatus.getSubmRecId());

}

}

关于java - Jacoco 需要对 Lambdas Java8 进行特殊处理吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45674950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com