junit - How to mock getCurrentLoginId in Spring security? -


i testing controller methods include calls with:

securityutils.getcurrentloginid() 

however, when run test

    securitycontext securitycontext = mockito.mock(securitycontext.class);     mockito.when(securityutils.getcurrentloginid()).thenreturn((long) 1);     securitycontextholder.setcontext(securitycontext);      requestbuilder requestbuilder = mockmvcrequestbuilders             .post(uri)             .with(user("admin").password("pass").roles("user","admin"))             .with(csrf())             .param("blogid", "" + blogid)             .content(maptojson(post))             .contenttype(mediatype.application_json)             .accept(mediatype.application_json); 

i get:

java.lang.illegalstateexception: user not found! 

you trying mock static method, , mockito doesn't support feature. please, use powermock instead.

you find examples of mocking static methods via powermock here.


Comments