Java 如何在Junit/Mockito中涵盖以下所有行

Java 如何在Junit/Mockito中涵盖以下所有行,java,spring-boot,mockito,code-coverage,junit4,Java,Spring Boot,Mockito,Code Coverage,Junit4,我试图模仿JdbcTemplate JdbcTemplate,但这并没有涵盖其中的任何内容 新员工(……) 请让我知道有没有办法涵盖新员工(…)中的这些行 公共列表findByCustIdAndType(长Id,字符串类型) { 返回jdbcTemplate.query(SQL.getEmployee(Id,type), (rs,rowNum)->新员工(rs.getLong(“客户ID”), rs.getLong(“主播客户ID”), rs.getString(“段”), rs.getDate

我试图模仿JdbcTemplate JdbcTemplate,但这并没有涵盖其中的任何内容

新员工(……)

请让我知道有没有办法涵盖新员工(…)中的这些行

公共列表findByCustIdAndType(长Id,字符串类型)
{
返回jdbcTemplate.query(SQL.getEmployee(Id,type),
(rs,rowNum)->新员工(rs.getLong(“客户ID”),
rs.getLong(“主播客户ID”),
rs.getString(“段”),
rs.getDate(“END_TS”);
}

尝试使用Mockito捕获lambda,这是一个
行映射器。然后使用模拟
ResultSet
设置调用它,以返回预期值,从而可以断言返回的
Employee
。下面是一个例子:

@RunWith(MockitoJUnitRunner.class)
public class EmployeeDAOTest {
    private static final long CUSTOMER_ID = 1;
    private static final long ANCHOR_CUSTOMER_ID = 2;
    private static final String SEGMENT = "A";
    private static final Date END_TS = Date.valueOf(LocalDate.now());

    @InjectMocks
    private EmployeeDAO dao;

    @Mock   
    private JdbcTemplate jdbcTemplate;

    @Mock   
    private ResultSet resultSet;

    @Captor
    private ArgumentCaptor<RowMapper<Employee>> rowMapperCaptor;

    @Before
    public void prepareTest() throws SQLException {
        when(resultSet.getLong("CUSTOMER_ID")).thenReturn(CUSTOMER_ID);
        when(resultSet.getLong("ANCHOR_CUSTOMER_ID")).thenReturn(ANCHOR_CUSTOMER_ID);
        when(resultSet.getString("SEGMENT")).thenReturn(SEGMENT);
        when(resultSet.getDate("END_TS")).thenReturn(END_TS);
    }

    @Test
    public void test() throws SQLException {
        dao.findByCustIdAndType(0, null);

        verify(jdbcTemplate).query(anyString(), rowMapperCaptor.capture());
        RowMapper<Employee> rowMapper = rowMapperCaptor.getValue();
        Employee employee = rowMapper.mapRow(resultSet, 1);
        assertEquals(CUSTOMER_ID, employee.getCustomerId());
        assertEquals(ANCHOR_CUSTOMER_ID, employee.getAnchorCustomerId());
        assertEquals(SEGMENT, employee.getSegment());
        assertEquals(END_TS, employee.getEndTs());
    }

}
@RunWith(MockitoJUnitRunner.class)
公共类EmployeeDAOTest{
私有静态最终长客户_ID=1;
私有静态最终长锚\u客户\u ID=2;
私有静态最终字符串段=“A”;
private static final Date END_TS=Date.valueOf(LocalDate.now());
@注射模拟
私人雇员道道;
@嘲弄
私有JdbcTemplate JdbcTemplate;
@嘲弄
私有结果集结果集;
@俘虏
私有参数捕获器rowMapperCaptor;
@以前
public void prepareTest()引发SQLException{
当(resultSet.getLong(“客户ID”))。然后返回(客户ID);
当(resultSet.getLong(“锚定客户ID”)。然后返回(锚定客户ID);
当(resultSet.getString(“段”))。然后返回(段);
when(resultSet.getDate(“结束”)),然后return(结束);
}
@试验
public void test()引发SQLException{
findByCustIdAndType(0,null);
验证(jdbcTemplate).query(anyString(),rowMapperCaptor.capture());
RowMapper RowMapper=rowMapperCaptor.getValue();
Employee=rowMapper.mapRow(结果集,1);
assertEquals(客户ID,employee.getCustomerId());
assertEquals(ANCHOR\u CUSTOMER\u ID,employee.getAnchorCustomerId());
assertEquals(段,employee.getSegment());
assertEquals(END_TS,employee.getEndTs());
}
}

尝试使用Mockito捕获lambda,这是一个
行映射器。然后使用模拟
ResultSet
设置调用它,以返回预期值,从而可以断言返回的
Employee
。下面是一个例子:

@RunWith(MockitoJUnitRunner.class)
public class EmployeeDAOTest {
    private static final long CUSTOMER_ID = 1;
    private static final long ANCHOR_CUSTOMER_ID = 2;
    private static final String SEGMENT = "A";
    private static final Date END_TS = Date.valueOf(LocalDate.now());

    @InjectMocks
    private EmployeeDAO dao;

    @Mock   
    private JdbcTemplate jdbcTemplate;

    @Mock   
    private ResultSet resultSet;

    @Captor
    private ArgumentCaptor<RowMapper<Employee>> rowMapperCaptor;

    @Before
    public void prepareTest() throws SQLException {
        when(resultSet.getLong("CUSTOMER_ID")).thenReturn(CUSTOMER_ID);
        when(resultSet.getLong("ANCHOR_CUSTOMER_ID")).thenReturn(ANCHOR_CUSTOMER_ID);
        when(resultSet.getString("SEGMENT")).thenReturn(SEGMENT);
        when(resultSet.getDate("END_TS")).thenReturn(END_TS);
    }

    @Test
    public void test() throws SQLException {
        dao.findByCustIdAndType(0, null);

        verify(jdbcTemplate).query(anyString(), rowMapperCaptor.capture());
        RowMapper<Employee> rowMapper = rowMapperCaptor.getValue();
        Employee employee = rowMapper.mapRow(resultSet, 1);
        assertEquals(CUSTOMER_ID, employee.getCustomerId());
        assertEquals(ANCHOR_CUSTOMER_ID, employee.getAnchorCustomerId());
        assertEquals(SEGMENT, employee.getSegment());
        assertEquals(END_TS, employee.getEndTs());
    }

}
@RunWith(MockitoJUnitRunner.class)
公共类EmployeeDAOTest{
私有静态最终长客户_ID=1;
私有静态最终长锚\u客户\u ID=2;
私有静态最终字符串段=“A”;
private static final Date END_TS=Date.valueOf(LocalDate.now());
@注射模拟
私人雇员道道;
@嘲弄
私有JdbcTemplate JdbcTemplate;
@嘲弄
私有结果集结果集;
@俘虏
私有参数捕获器rowMapperCaptor;
@以前
public void prepareTest()引发SQLException{
当(resultSet.getLong(“客户ID”))。然后返回(客户ID);
当(resultSet.getLong(“锚定客户ID”)。然后返回(锚定客户ID);
当(resultSet.getString(“段”))。然后返回(段);
when(resultSet.getDate(“结束”)),然后return(结束);
}
@试验
public void test()引发SQLException{
findByCustIdAndType(0,null);
验证(jdbcTemplate).query(anyString(),rowMapperCaptor.capture());
RowMapper RowMapper=rowMapperCaptor.getValue();
Employee=rowMapper.mapRow(结果集,1);
assertEquals(客户ID,employee.getCustomerId());
assertEquals(ANCHOR\u CUSTOMER\u ID,employee.getAnchorCustomerId());
assertEquals(段,employee.getSegment());
assertEquals(END_TS,employee.getEndTs());
}
}

谢谢!!!完美的你能解释一下这是怎么工作的吗?比如说,在什么位之后被调用。还有捕获者是如何工作的?不确定你的意思,只是添加断点来查看调用顺序。captor允许捕获注入的mock方法的参数——在本例中是由lambda定义的行映射器。调用verify()时将填充捕获器。之后,可以根据需要访问和使用捕获的参数。在本例中,它是一个行映射器,因此它的mapRow()方法被调用,这样就可以断言结果(一个
员工
)。谷歌的教程和例子。谢谢!!!完美的你能解释一下这是怎么工作的吗?比如说,在什么位之后被调用。还有捕获者是如何工作的?不确定你的意思,只是添加断点来查看调用顺序。captor允许捕获注入的mock方法的参数——在本例中是由lambda定义的行映射器。调用verify()时将填充捕获器。之后,可以根据需要访问和使用捕获的参数。在本例中,它是一个行映射器,因此它的mapRow()方法被调用,这样就可以断言结果(一个
员工
)。谷歌的教程和例子。