Jacoco need special handling for Lambdas Java8?

Viewed 2253

I have read several old posts about Jacoco supporting Lambda functions and issue is addressed a couple years ago.

I am finding that when I run Jacoco, it does not report coverage for the Lambda function in this code

        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"))
    );

I know it is being hit because the test couldn't pass.
Do I need to do something special for Jacoco to report the coverage properly?

1 Answers

You can try something like this to get 100% coverage.

On the source code, you can modify like this

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")
    );
  }

And you can create Junit Test class with Mockito to make it work

@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());

    }

}
Related