'How to mock jdbcTemplate.query(query.toString(), resultSet -> {});

My Repository code:

jdbcTemplate.query(query.toString(), resultSet -> {
                PartGroup partGroup = new PartGroupMapper().mapRow(resultSet, resultSet.getRow());
                partGroup.setDeclaredCompliance(resultSet.getString("declared_compliance"));
                partGroup.setCalculatedCompliance(resultSet.getString("calculated_compliance"));
                partGroup.setStatus(resultSet.getString("status"));
                partGroup.setSpecificationId(null == resultSet.getObject("specification_id") ? null : resultSet.getLong("specification_id"));
                if (null != partGroup.getSpecificationId())
                    partGroup.setSpecificationName(specificationLookup.get(partGroup.getSpecificationId()).getSpecificationName());
                partGroups.add(partGroup);
            });

I want to return list of partGroups which contains partGroup object. What I'm trying is:

PowerMockito.when(jdbcTemplate.query(ArgumentMatchers.anyString(),ArgumentMatchers.any(PartGroupMapper.class))).thenReturn(list);
            PowerMockito.when(jdbcTemplate.query(ArgumentMatchers.anyString(),ArgumentMatchers.any(RowMapper.class))).thenAnswer(invocation -> {
                RowMapper<PartGroup> rowMapper = invocation.getArgument(1);

                ResultSet rs = PowerMockito.mock(ResultSet.class);

                PowerMockito.when(rs.getString("declared_compliance")).thenReturn("Compliant");
                PowerMockito.when(rs.getString("calculated_compliance")).thenReturn(null);
                PowerMockito.when(rs.getString("status")).thenReturn("Active");
                PowerMockito.when(rs.getString("specification_id")).thenReturn(null);

                list.add(rowMapper.mapRow(rs,0));
                return list;
            });


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source