java - Mock SpringContextHolder using MockMVC -
i writing unit test cases controller layer. have call getting user spring securitycontextholder. when run test case null pointer exception because don't know how mock spring security context.
below code, suggestion how it?
controller methhod:
@requestmapping(method = requestmethod.post) public void savesettings(@requestbody emailsettingdto emailsetting) { user user = ((currentuser) securitycontextholder.getcontext().getauthentication().getprincipal()).getuser(); settings.saveuseremailsetting(user, emailsetting); }
my test case :
@test public void testsavesettings() throws exception { mockmvc.perform(post(base_url).content(this.objectmapper.writevalueasstring(emaildto)) .contenttype(mediatypes.hal_json)).andexpect(status().isok());
}
there spring security test library purpose.
you can use @withmockuser
achieve this. see post
Comments
Post a Comment