【发布时间】:2020-07-22 13:18:12
【问题描述】:
我知道为 mapper 编写单元测试或设置不是一件好事,但它就是这样,所以我被困在如何为 mapper 进行单元测试;
学生组列表如下;
@Getter
@Setter
public class StudentGroupList {
private String studentId;
}
StudentGroupListRowMapper 下面;
public class StudentGroupListRowMapper implements RowMapper<StudentGroupList> {
@Override
public StudentGroupList mapRow(Resultset rs, int rowNum) throws SQLException {
StudentGroupList studentGroupList = new StudentGroupList();
studentGroupList.setStudentId(rs.getString("student_id"));
return studentGroupList;
}
}
我在下面尝试过,但是 jococo 覆盖率测试没有覆盖任何东西
public class TaskGroupListRowMapperTest {
private ResultSet resultSet;
private StudentGroupList studentGroupList;
@Before
public void setUp() {
resultSet = mock(ResultSet.class);
studentGroupList = mock(StudentGroupList.class);
}
@Test
public void testStudentGroupListMapper() throws SQLException {
when(resultSet.getString("student_id"))
.thenReturn(studentGroupList.getStudentID());
assertTrue(studentGroupList.getStudentId(), true);
}
}
它说异常; thenReturn() 可能会丢失。
【问题讨论】:
-
我的意思是 DAO 层通常没有经过测试,但它比上次 (stackoverflow.com/questions/63001810/…) 当你想测试构造函数时有了很大的改进,所以还不错:) 你仍然会得到减号投票(不是来自我,但仍然是),因为您不是针对特定问题寻求帮助,您只是希望我们为您编写测试
-
感谢您的回答,但下次我可以自己编写测试,因为我从未为 mapper 编写过单元测试
-
@JAsgarow 请查看我的更新答案
-
那个测试类完全没有任何意义。为什么它实现
RowMapper?你想测试StudentGroupListRowMapper然后测试它。目前你什么都没有测试(这也是 Jacoco 告诉你的!)。为ResultSet创建一个模拟(使用类似 Mockito 的东西)。调用 rowmapper 上的方法并断言结果是正确的。
标签: arrays spring spring-boot unit-testing jdbc