junit - Play2 Java - InvalidPropertyException when testing controller with form -
i'd test controller method named authenticate()
, has simple logic: validating email
, password
request , returning result json.
public class users extends controller { static form<user> userform = form.form(user.class); public static result login() { return ok(views.html.users.login.render(userform)); } public static result authenticate() { form<user> filledform = userform.bindfromrequest(); if (filledform.haserrors()) { return badrequest(views.html.users.login.render(filledform)); } else { objectnode result = json.newobject(); user u = filledform.get(); if (user.isauthvalid(u.email, u.password)) result.put("status", "ok"); else result.put("status", "authentication failed"); return ok(result); } } }
following test code authenticate()
:
@test public void callauthenticate() { map<string, string> formdata = maps.newhashmap(); formdata.put("email", "aaa@bbb.com"); formdata.put("password", "password"); result result = callaction(controllers.routes.ref.users.authenticate(), fakerequest().withformurlencodedbody(formdata)); assertthat(status(result)).isequalto(http.status.ok); }
but got error following stacktrace:
javax.validation.validationexception: hv000041: call traversableresolver.isreachable() threw exception. @ org.hibernate.validator.internal.engine.validatorimpl.isvalidationrequired(validatorimpl.java:1230) @ org.hibernate.validator.internal.engine.validatorimpl.validateconstraint(validatorimpl.java:438) @ org.hibernate.validator.internal.engine.validatorimpl.validateconstraintsfordefaultgroup(validatorimpl.java:387) @ org.hibernate.validator.internal.engine.validatorimpl.validateconstraintsforcurrentgroup(validatorimpl.java:351) @ org.hibernate.validator.internal.engine.validatorimpl.validateincontext(validatorimpl.java:303) @ org.hibernate.validator.internal.engine.validatorimpl.validate(validatorimpl.java:133) @ org.springframework.validation.beanvalidation.springvalidatoradapter.validate(springvalidatoradapter.java:194) @ play.data.form.bind(form.java:327) @ play.data.form.bindfromrequest(form.java:215) @ controllers.users.authenticate(users.java:50) @ controllers.ref.reverseusers$$anonfun$authenticate$1.apply(routes_reverserouting.scala:477) @ controllers.ref.reverseusers$$anonfun$authenticate$1.apply(routes_reverserouting.scala:477) @ play.core.router$handlerinvoker$$anon$6$$anon$2.invocation(router.scala:164) @ play.core.j.javaaction$$anon$1.call(javaaction.scala:31) @ play.core.j.javaaction$$anon$2.apply(javaaction.scala:74) @ play.core.j.javaaction$$anon$2.apply(javaaction.scala:73) @ play.libs.f$promise$promiseactor.onreceive(f.java:420) @ akka.actor.untypedactor$$anonfun$receive$1.applyorelse(untypedactor.scala:159) @ akka.actor.actorcell.receivemessage(actorcell.scala:425) @ akka.actor.actorcell.invoke(actorcell.scala:386) @ akka.dispatch.mailbox.processmailbox(mailbox.scala:230) @ akka.dispatch.mailbox.run(mailbox.scala:212) @ akka.dispatch.forkjoinexecutorconfigurator$mailboxexecutiontask.exec(abstractdispatcher.scala:502) @ scala.concurrent.forkjoin.forkjointask.doexec(forkjointask.java:262) @ scala.concurrent.forkjoin.forkjoinpool$workqueue.runtask(forkjoinpool.java:975) @ scala.concurrent.forkjoin.forkjoinpool.runworker(forkjoinpool.java:1478) @ scala.concurrent.forkjoin.forkjoinworkerthread.run(forkjoinworkerthread.java:104) caused by: org.springframework.beans.invalidpropertyexception: invalid property 'email' of bean class [models.user]: no property 'email' found @ org.springframework.beans.beanwrapperimpl.getpropertydescriptor(beanwrapperimpl.java:337) @ play.db.ebean.model._idaccessors(model.java:47) @ play.db.ebean.model._getid(model.java:67) @ play.db.ebean.model.hashcode(model.java:208) @ org.hibernate.validator.internal.engine.resolver.singlethreadcachedtraversableresolver$traversableholder.buildhashcode(singlethreadcachedtraversableresolver.java:153) @ org.hibernate.validator.internal.engine.resolver.singlethreadcachedtraversableresolver$traversableholder.<init>(singlethreadcachedtraversableresolver.java:114) @ org.hibernate.validator.internal.engine.resolver.singlethreadcachedtraversableresolver$traversableholder.<init>(singlethreadcachedtraversableresolver.java:96) @ org.hibernate.validator.internal.engine.resolver.singlethreadcachedtraversableresolver.isreachable(singlethreadcachedtraversableresolver.java:41) @ org.hibernate.validator.internal.engine.validatorimpl.isvalidationrequired(validatorimpl.java:1221) ... 26 more
when testing on browser, worked expected. junit test fails invalidpropertyexception
. what's wrong test code?
fyi, here's model user
:
@entity public class user extends model { @id @required @nonempty public string email; public string nickname; @required public string password; public string salt; public static finder<string, user> find = new finder<string, user>( string.class, user.class); public static user findbyemail(string email) { return find.where().eq("email", email).findunique(); } public static boolean isauthvalid(string email, string password) { user user = findbyemail(email); if (user == null) return false; return user.isvalidpassword(password); } public boolean isvalidpassword(string password) { return this.password.equals(digestutils.md5hex(password + this.salt)); } }
thanks advices/corrections.
you need run test inside "fake" application, binding can work. so, you're test this:
@test public void callauthenticate() { running(fakeapplication(), new runnable() { public void run() { map<string, string> formdata = maps.newhashmap(); formdata.put("email", "aaa@bbb.com"); formdata.put("password", "password"); result result = callaction(controllers.routes.ref.users.authenticate(), fakerequest().withformurlencodedbody(formdata)); assertthat(status(result)).isequalto(http.status.ok); } } }
Comments
Post a Comment