Write a happy-path test for the class presented below. Verify that the user gets
ID: 3595698 • Letter: W
Question
Write a happy-path test for the class presented below. Verify that the user gets his new password, and
that the updateUser() method of userDAO is called.
Listing 5.43. The UserServiceImpl class
public class UserServiceImpl {
private UserDAO userDAO;
private SecurityService securityService;
public void assignPassword(User user) throws Exception {
String passwordMd5 = securityService.md5(user.getPassword());
user.setPassword(passwordMd5);
userDAO.updateUser(user);
}
public UserServiceImpl(UserDAO dao, SecurityService security) {
this.userDAO = dao;
this.securityService = security;
}
}
Explanation / Answer
package dto;
public class UserDTO
{
private String password;
private int id;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
package dao;
import dto.UserDTO;
public interface UserDAO
{
public void updateUser(UserDTO dto);
}
import dto.UserDTO;
import dao.UserDAO;
class UserDAOImpl implements UserDAO
{
public int updateUser(UserDTO dto){
int result ;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
String query = UPDATE userdetails SET password = ? AND userid= ?
PreparedStatement pstmt=con.prepareStatement(query);
pstmt.setString(dto.getPassword());
pstmt.setInt(dto.getId());
result=pstmt.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}
finally{
rs.close();
pstmt.claos();
con.close();
}
return result;
}
}
import java.security.*;
import dao.UserDAO;
import dto.UserDTO;
public class UserServiceImpl
{
private UserDAO userDAO;
private SecurityService securityService;
public UserServiceImpl(UserDAO dao,SecurityService security){
this.userDAO = dao;
this.securityService =security;
}
public void assignPassword(UserDTO userDTO) throws Exception{
UserDAO dao = new UserDAOImpl();
String passwordMd5 = securityService.md5(userDTO.getPassword());
userDTO.setPassword(passwordMd5);
dao.updateUser(userDTO);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.