How to use Mockito to retrieve userInfo from the session?

1,026 views
Skip to first unread message

Alex Kulczycki

unread,
Sep 16, 2013, 3:31:43 PM9/16/13
to moc...@googlegroups.com
We know how to set up a mock HTTPSession object, but how can we test code that depends on the session attributes?
 
(Purpose:
We already have functional tests that exercise this code using selenium, and we are already have test coverage of most of this class, but there are some corner cases where we need to test methods that call session.getAttribute().  This is a simple example but in our production code, there is business information stored in the session variables that we need to test.  At times jQuery and/or JavaScript may use the session attributes.)

 

Here is the object UserName

 

public class UserName {

      private String firstName;

      private String lastName;

     

 

public String getFirstName() {

            return firstName;

      }

 

      public void setFirstName(String firstName) {

            this.firstName = firstName;

      }

 

      public String getLastName() {

            return lastName;

      }

 

      public void setLastName(String lastName) {

            this.lastName = lastName;

      }

}

 

The session set up is:

session.setAttribute("UserInfo",UserName);

 

Question:

How to use Mockito to retrieve userInfo from the session?

 

The methods we want to test depend on the value of session.setAttribute("UserInfo").  Currently, the value is null, which gives us JUnit errors.

 
Thanks,
Alex

Eric Lefevre-Ardant

unread,
Sep 17, 2013, 4:26:30 AM9/17/13
to moc...@googlegroups.com
"How to use Mockito to retrieve userInfo from the session?"
I'm not sure I understand the question. It's your production code that would retrieve the userInfo. Why would you need Mockito to do that?

Anyway, how about passing an instance of Session (that contains the appropriate instance of UserName) yourself? 

In the end, the complexity lies not in the UserName object, but in the code that make use of the session. A snippet of this code might be helpful.


--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages