You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Miller, John" <jm...@litle.com> on 2006/02/15 23:01:41 UTC

Shale Test Framework setting the remote User

Hi, 

I am new to shale and the concept of mock objects, I am trying to write
a unit test to test a JSF action. The action requires the
getRemoteUser() to return a valid user. Can anyone tell me the proper
way to set up a user in the Mock External context. Thank you in advance.

 

John 
 
 
 
NOTICE:  This message, including all attachments transmitted with it, is for the use of the addressee only. It may contain proprietary, confidential and/or legally privileged information belonging to Litle & Co. No confidentiality or privilege is waived or lost by any mistransmission. If you are not the intended recipient, you must not, directly or indirectly, use, disclose, distribute, print or copy any part of this message.  If you believe you have received this message in error, please delete it and all copies of it from your system and notify the sender immediately by reply e-mail.  Thank you. 

Re: Shale Test Framework setting the remote User

Posted by Craig McClanahan <cr...@apache.org>.
On 2/15/06, Miller, John <jm...@litle.com> wrote:
>
> Hi,
>
> I am new to shale and the concept of mock objects, I am trying to write
> a unit test to test a JSF action. The action requires the
> getRemoteUser() to return a valid user. Can anyone tell me the proper
> way to set up a user in the Mock External context. Thank you in advance.


The mock external context calls getRemoteUser() on the  mock servlet request
object, so that is where we really need to set it.  If your unit test case
is a subclass of AbstractJsfTestCase  or AbstractViewControllerTestCase, the
superclass will have set you up an instance variable called "request" for
this, so all you need to do is:

    request.setUserPrincipal(new MyPrincipal("username"));

where MyPrincipal is a class that implements the
java.security.Principalinterface.  (Obviously that's not very friendly
... I'll add a MockPrincipal
class that should be available in tonight's nightly build.)

Setting this propery will cause both request.getUserPrincipal() and
request.getRemoteUser() to return non-null values, which can then be
accessed via ExternalContext.getUserPrincipal() and
ExternalContext.getRemoteUser() respectively.

John


Craig