Dario Dario - 9 days ago 11
Java Question

Gwt Junit test HttpServletRequest Nullpointer

I'm doing this project with GWT and when i make the Junit test for the LogIn method i get a NullPointerException because i check the session with HttpServletRequest.

Here the Junit test

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"));*/
}


And here the method checkLogin from UsersServiceImpl

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);
}


The storeUserInSession return null and i get the NullPointerException.

How can i avoid this, or how can i call an HttpServletRequest for session in Junit test?

Ok changed the method storeUserInSession with

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();
}

}


Or can be good this one too

private void storeUserInSession(UserDTO user) {

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

Answer

There different solutions to your problem:

  1. You could change the test to become a GWT Testcase, this would be run in the browser as compiled JavaScript.
  2. You could change the visibility of storeUserInSession to protected, derive from that Class and change the method for your test.
  3. You could use a mocking framework like "Powermockito" to mock the Session
  4. You could make storeUserInSession NullPointer safe and return true or false, if the data was stored.

I would use way 1 or 4, because this would be the easiest ways.

The most elegant way is 3, I think, but you would need to learn a mocking framework.

Comments