JUnit Testing Controller with Ebean. Setting of attributes is not working via direct field access - only via setter.

16 views
Skip to first unread message

Simon Jacobs

unread,
Apr 29, 2016, 5:36:34 AM4/29/16
to play-framework
Hey guys,

we currently have a very strange behaviour in our Controller tests. It seems, that the direct field access is not working. There is no error message. Just the property are not set.

The "passwordResetTokenExpiresAt" property in the action don't affect the users entity. I stepped over the lines in debugging mode and the property is definitely not changing. Never seen something like this before.
Also: If I just click Junit->Run the test fails at the last Assertion (check the passwordResetTokenExpiresAt). If I run the test in debugging mode the test fails at the first Assertion (check if the new password has been set).

Does anyone know what happens here? I have a suspicion, that the post compile of ebean is involved, but I'm not sure and don't know what to do. Google said, that in previous Play version the line
compile in Test <<= PostCompile(Test)
enable the direct field access in tests, but it's not working anymore. I also guess, that if the direct field access is not working an Exception will be thrown .

Thanks and best regards
Simon


Test:

@Test
   
public void sendResetTokenAndSetNewPassword() throws Exception
   
{
       
       
User user = User.find.byId(1);
        assertNotNull
(user);
       
       
Map<String, String> data = new HashMap<>();
        data
.put("email", user.email);
       
Helpers.route(requestBuilder(data, routes.LoginController.sendResetToken(), Helpers.POST));
       
        user
.refresh();
       
//Check, that the sendResetToken action has created a Token
        assertNotNull
(user.passwordResetToken);
        assertNotNull
(user.passwordResetTokenExpiresAt);

       
//Render view and generate CSRF      
       
Result result = Helpers.route(requestBuilder(null, routes.LoginController.showResetPassword(user.passwordResetToken), Helpers.GET));
       
       
Map<String, String> newPasswordData = new HashMap<>();
        newPasswordData
.put("password", "newSecret");
        newPasswordData
.put("passwordConfirmation", "newSecret");
       
       
//Set tokens to null and set new Password
       
Helpers.route(requestBuilder(newPasswordData, routes.LoginController.setNewPassword(user.passwordResetToken), Helpers.POST), TIMEOUT);
       
        user
.refresh();
       
        assertTrue
(user.checkPassword("newSecret")); // is working!
        assertNull
(user.passwordResetToken); // is working!
        assertNull
(user.passwordResetTokenExpiresAt); // is NOT working!
   
}

The action where the three attributes are set. (The new password via getter, the PasswordResetToken via getter and the passwordResetTokenExpiresAt via direct access)

 public Result setNewPassword(String token)
   
{
       
User user = User.findByToken(token);
       
Form<NewPasswordForm> form = formFactory.form(NewPasswordForm.class).bindFromRequest();
       
       
if (user != null && !form.hasErrors()) {
           
NewPasswordForm newPasswordForm = form.get();
           
           
if (newPasswordForm.password.equals(newPasswordForm.passwordConfirmation) && !user.isPasswordResetTokenExpired()) {
                user
.setPassword(newPasswordForm.password);
               
               
// user.passwordResetToken = null;
                user
.passwordResetTokenExpiresAt = null;
                user
.setPasswordResetToken(null);
               
                user
.save();
               
return redirect(routes.LoginController.index());
           
}
           
       
}
       
return ok(views.html.web.login.resetPassword.render(formFactory.form(NewPasswordForm.class), user, token));
   
}



Reply all
Reply to author
Forward
0 new messages