gpt4 book ai didi

java - Gwt Junit 测试 HttpServletRequest Nullpointer

转载 作者:行者123 更新时间:2023-11-30 07:01:56 25 4
gpt4 key购买 nike

我正在使用 GWT 做这个项目,当我对 LogIn 方法进行 Junit 测试时,我得到一个 NullPointerException,因为我使用 HttpServletRequest 检查 session 。

这里是 Junit 测试

private void checkLoginTest() {

UsersServiceImpl USI = new UsersServiceImpl();

UserDTO userDTO1 = new UserDTO();
UserDTO userDTO2 = new UserDTO();

User user1 = new User("username1", "password1", "email1",
"aa", "aa", "aa", "aa", UserRights.USER);
User user2 = new User("username2", "password2", "email2",
"aa", "aa", "aa", "aa", UserRights.USER);

userDTO1.setUser(user1);
userDTO2.setUser(user2);

UserDTO insertedUser1 = USI.register(userDTO1);
UserDTO insertedUser2 = USI.register(userDTO2);

/*
* Check login success
*/
Assert.assertNotNull(USI.checkLogin("username1", "password1"));
Assert.assertNotNull(USI.checkLogin("username2", "password2"));
//Wrong password below
Assert.assertNull(USI.checkLogin("username2", "ciaone"));

/*
* Check user returned by login
*/
/*Assert.assertEquals(user1.getUsername(), USI.checkLogin("username1", "password1"));
Assert.assertEquals(user2.getUsername(), USI.checkLogin("username2", "password2"));*/
}

这里是来自 UsersServiceImpl 的 checkLogin 方法

public UserDTO checkLogin(String username, String password) {

// boolean check = false;
UserDTO checkedUser = new UserDTO();

mapDB.begin();

Map<String, User> userMap = mapDB.getDB().createTreeMap("UserMap")
.makeOrGet();

if (userMap.containsKey(username)) {
// Controllo se la password è corretta per quello username
checkedUser.setUser(userMap.get(username));

if (checkedUser.getUser().getPassword().equals(password)) {
// check = true;
checkedUser.setLogged(true);
// Salvo la sessione utente
storeUserInSession(checkedUser);
} else {

checkedUser.setUser(null);
}

} else {

checkedUser.setUser(null);
}

mapDB.end();

return checkedUser;
}

private void storeUserInSession(UserDTO user) {
HttpServletRequest httpServletRequest = this.getThreadLocalRequest();
HttpSession session = httpServletRequest.getSession(true);
session.setAttribute("user", user);
}

storeUserInSession 返回 null,我得到 NullPointerException。

如何避免这种情况,或者如何在 Junit 测试中调用 HttpServletRequest 进行 session ?

确定将方法 storeUserInSession 更改为

private void storeUserInSession(UserDTO user) {
//Junit test ritorna NullPointerException
//In questo modo funziona
try {
HttpServletRequest httpServletRequest = this.getThreadLocalRequest();
HttpSession session = httpServletRequest.getSession(true);
session.setAttribute("user", user);
} catch (Exception e) {
System.err.println("Errore creazione HttpServeletRequest");
e.printStackTrace();
}

}

或者这个也不错

private void storeUserInSession(UserDTO user) {

HttpServletRequest httpServletRequest = this.getThreadLocalRequest();
if( httpServletRequest != null){
HttpSession session = httpServletRequest.getSession(true);
session.setAttribute("user", user);
}
}

最佳答案

您的问题有不同的解决方案:

  1. 您可以将测试更改为 GWT 测试用例,这将作为已编译的 JavaScript 在浏览器中运行。
  2. 您可以将 storeUserInSession 的可见性更改为 protected,从该 Class 派生并更改测试方法。
  3. 您可以使用“Powermockito”之类的模拟框架来模拟 session
  4. 如果数据已存储,您可以使 storeUserInSession NullPointer 安全并返回 true 或 false。

我会使用方法 1 或 4,因为这是最简单的方法。

我认为最优雅的方式是 3,但是你需要学习一个模拟框架。

关于java - Gwt Junit 测试 HttpServletRequest Nullpointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40774388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com