Hướng dẫn kiểm thử lớp DAO

Trường mình ra đề kiểm thử lớp DAO bằng Junit4 mà chưa dạy kĩ về phần Mockito và PowerMock. Mình mong hỗ trợ về cách sử dụng PowerMock và Mockito
Lớp DAO

public ArrayList<User> getListUser() throws SQLException {
    Connection connection = DBConnect.getConnecttion();
    String sql = "SELECT * FROM users";
    PreparedStatement ps = connection.prepareCall(sql);
    ResultSet rs = ps.executeQuery();
    ArrayList<User> list = new ArrayList<User>();
    while (rs.next()) {
        User user = new User();
       user.setUserID(rs.getLong("user_id"));
       user.setUserName(rs.getString("user_name"));
       user.setUserEmail(rs.getString("user_email"));
       user.setUserPass(rs.getString("user_pass"));
       user.setUserRole(rs.getBoolean("user_role"));
       user.setUserPhone(rs.getString("user_phone"));
       list.add(user);
    }
    return list;
}

Search cái ra liền mà bạn.
https://dzone.com/articles/mockito-basic-example-using-jdbc

4 Likes

Code bị lỗi

when(db.getConnecttion()).thenReturn(c);
		when(c.prepareCall(any(String.class))).thenReturn(stmt);

Lỗi

The method prepareCall(String) in the type Connection is not applicable for the arguments (Matcher<String>)

Lỗi báo rồi đấy bạn!

Phướng thức cần tham số kiểu String mà lại truyền vào kiểu Matcher<String>.

4 Likes

Cảm ơn bạn mình đã fix được nhưng lại bị lỗi The method thenReturn(Integer) in the type OngoingStubbing is not applicable for the arguments (ResultSet)

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?