The easiest way to do this is to use the remoteUser. In your test:
MockHttpServletRequest request = ...
request.setRemoteUser("foo");
Then in your Controller, use:
String username = request.getRemoteUser();
Matt
On 11/28/06, Man-Chi Leung <[EMAIL PROTECTED]> wrote:
hi,
in my spring mvc controller, I need to get username from acegi security
context and pass this username to my service manager. but bcos of this
acegi dependency, I have difficulty to do unit test for my mvc
controller.
may I know what is the best practice?
should I refactor the mvc controller, injecting denpendency of
acegi security context to my Controller via spring from
setAcegiContext(), then use Jmock to mock a acegi security context in
my MyControllerTest ?
i am a bit puzzled on this. pls advise. thanks so much.
~thinkboy.
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
User currentUser = (User) SecurityContextHolder.getContext()
.getAuthentication().getPrincipal();
String username = currentUser.getUsername()
return new ModelAndView("serviceList", Constants.SERVICE_LIST,
mgr
.lookupServicesByUsername(username));
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
http://raibledesigns.com
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]