java - Error when testing secured spring repository -
i'm having problem unit test spring jpa repository.
i added method repository should return entities logged in user:
@query("select budget budget budget budget.user.login = ?#{principal.username}") list<budget> findbyuseriscurrentuser();
budget resource:
/** * /budgets : budgets. * * @return responseentity status 200 (ok) , list of budgets in body */ @getmapping("/budgets") @timed public list<budget> getallbudgets() { log.debug("rest request budgets"); return budgetrepository.findbyuseriscurrentuser(); }
i adjusted test getallbudgets , added .with(user) part:
@test @transactional public void getallbudgets() throws exception { budgetrepository.saveandflush(budget); // create security-aware mockmvc restbudgetmockmvc = mockmvcbuilders .webappcontextsetup(context) .apply(springsecurity()) .build(); budgetrepository.saveandflush(budget); // points restbudgetmockmvc.perform(get("/api/budgets?sort=id,desc") .with(user("user").roles("user"))) .andexpect(status().isok()) .andexpect(content().contenttype(mediatype.application_json_utf8_value)) .andexpect(jsonpath("$.[*].id").value(hasitem(budget.getid().intvalue()))) .andexpect(jsonpath("$.[*].name").value(hasitem(default_name.tostring()))); }
but test fails https://pastebin.com/8aldtauc:
<testcase name="getallbudgets" classname="com.pahofmann.budget.web.rest.budgetresourceinttest" time="0.19"> <failure message="java.lang.assertionerror: json path "$.[*].id" expected: collection containing <7> but: " type="java.lang.assertionerror"> java.lang.assertionerror: json path "$.[*].id" expected: collection containing <7> but: @ org.hamcrest.matcherassert.assertthat(matcherassert.java:20) @ org.springframework.test.util.jsonpathexpectationshelper.assertvalue(jsonpathexpectationshelper.java:74) @ org.springframework.test.web.servlet.result.jsonpathresultmatchers$1.match(jsonpathresultmatchers.java:87) @ org.springframework.test.web.servlet.mockmvc$1.andexpect(mockmvc.java:171) @ com.pahofmann.budget.web.rest.budgetresourceinttest.getallbudgets(budgetresourceinttest.java:177) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:498) @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:50) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:47) @ org.junit.internal.runners.statements.invokemethod.evaluate(invokemethod.java:17) @ org.junit.internal.runners.statements.runbefores.evaluate(runbefores.java:26) @ org.springframework.test.context.junit4.statements.runbeforetestmethodcallbacks.evaluate(runbeforetestmethodcallbacks.java:75) @ org.springframework.test.context.junit4.statements.runaftertestmethodcallbacks.evaluate(runaftertestmethodcallbacks.java:86) @ org.springframework.test.context.junit4.statements.springrepeat.evaluate(springrepeat.java:84) @ org.junit.runners.parentrunner.runleaf(parentrunner.java:325) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:252) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:94) @ org.junit.runners.parentrunner$3.run(parentrunner.java:290) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:71) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:288) @ org.junit.runners.parentrunner.access$000(parentrunner.java:58) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268) @ org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61) @ org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:70) @ org.junit.runners.parentrunner.run(parentrunner.java:363) @ org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:191) @ org.gradle.api.internal.tasks.testing.junit.junittestclassexecuter.runtestclass(junittestclassexecuter.java:114) @ org.gradle.api.internal.tasks.testing.junit.junittestclassexecuter.execute(junittestclassexecuter.java:57) @ org.gradle.api.internal.tasks.testing.junit.junittestclassprocessor.processtestclass(junittestclassprocessor.java:66) @ org.gradle.api.internal.tasks.testing.suitetestclassprocessor.processtestclass(suitetestclassprocessor.java:51) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:498) @ org.gradle.internal.dispatch.reflectiondispatch.dispatch(reflectiondispatch.java:35) @ org.gradle.internal.dispatch.reflectiondispatch.dispatch(reflectiondispatch.java:24) @ org.gradle.internal.dispatch.contextclassloaderdispatch.dispatch(contextclassloaderdispatch.java:32) @ org.gradle.internal.dispatch.proxydispatchadapter$dispatchinginvocationhandler.invoke(proxydispatchadapter.java:93) @ com.sun.proxy.$proxy1.processtestclass(unknown source) @ org.gradle.api.internal.tasks.testing.worker.testworker.processtestclass(testworker.java:109) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:498) @ org.gradle.internal.dispatch.reflectiondispatch.dispatch(reflectiondispatch.java:35) @ org.gradle.internal.dispatch.reflectiondispatch.dispatch(reflectiondispatch.java:24) @ org.gradle.internal.remote.internal.hub.messagehubbackedobjectconnection$dispatchwrapper.dispatch(messagehubbackedobjectconnection.java:146) @ org.gradle.internal.remote.internal.hub.messagehubbackedobjectconnection$dispatchwrapper.dispatch(messagehubbackedobjectconnection.java:128) @ org.gradle.internal.remote.internal.hub.messagehub$handler.run(messagehub.java:404) @ org.gradle.internal.concurrent.executorpolicy$catchandrecordfailures.onexecute(executorpolicy.java:63) @ org.gradle.internal.concurrent.stoppableexecutorimpl$1.run(stoppableexecutorimpl.java:46) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1149) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:624) @ org.gradle.internal.concurrent.threadfactoryimpl$managedthreadrunnable.run(threadfactoryimpl.java:55) @ java.lang.thread.run(thread.java:748) </failure>
it seems there no value in array, working fine without user part. application working fine, need change test working user?
your test initialization wrong: budget
not attached user, that's why repository method returns empty list.
user user = userrepository.findonebylogin("user").get(); budget.setuser(user); budgetrepository.saveandflush(budget);
using @query
principal
makes difficult debug, prefer more explicit approach login provided resource controller:
@getmapping("/budgets") public responseentity<list<budget>> getmybudgets() { list<budget> budgets = budgetrepository.findbyuserlogin(securityutils.getcurrentuserlogin()); return responseentity.ok().body(budgets); }
and repository no longer needs @query
:
public interface budgetrepository extends jparepository<budget,long> { list<budget> findall(); list<budget> findbyuserlogin(string login); }
as bonus, can use same method administrator want find budgets of user.
Comments
Post a Comment