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

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -