Package org.springframework.mock.web

# Class MockHttpSession

java.lang.Object
org.springframework.mock.web.MockHttpSession

All Implemented Interfaces:
  `HttpSession`

public class MockHttpSession extends Object implements HttpSession
Mock implementation of the `HttpSession` interface.

As of Spring 6.0, this set of mocks is designed on a Servlet 6.0 baseline.

Since:
  1.0.2
Author:
  Juergen Hoeller, Rod Johnson, Mark Fisher, Sam Brannen, Vedran Pavic

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`SESSION_COOKIE_NAME`
The session cookie name.

## Constructor Summary

Constructors
Constructor
Description
`MockHttpSession()`
Create a new MockHttpSession with a default `MockServletContext`.
`MockHttpSession(ServletContext servletContext)`
Create a new MockHttpSession.
`MockHttpSession(ServletContext servletContext, String id)`
Create a new MockHttpSession.

## Method Summary

Modifier and Type
Method
Description
`void`
`access()`
`String`
`changeSessionId()`
As of Servlet 3.1, the id of a session can be changed.
`void`
`clearAttributes()`
Clear all of this session's attributes.
`void`
`deserializeState(Serializable state)`
Deserialize the attributes of this session from a state object created by `serializeState()`.
`Object`
`getAttribute(String name)`
`Enumeration<String>`
`getAttributeNames()`
`long`
`getCreationTime()`
`String`
`getId()`
`long`
`getLastAccessedTime()`
`int`
`getMaxInactiveInterval()`
`ServletContext`
`getServletContext()`
`void`
`invalidate()`
Invalidates this session then unbinds any objects bound to it.
`boolean`
`isInvalid()`
`boolean`
`isNew()`
`void`
`removeAttribute(String name)`
`Serializable`
`serializeState()`
Serialize the attributes of this session into an object that can be turned into a byte array with standard Java serialization.
`void`
`setAttribute(String name, Object value)`
`void`
`setMaxInactiveInterval(int interval)`
`void`
`setNew(boolean value)`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### SESSION_COOKIE_NAME

public static final String SESSION_COOKIE_NAME
The session cookie name.

See Also:
  - Constant Field Values

## Constructor Details

### MockHttpSession

public MockHttpSession()
Create a new MockHttpSession with a default `MockServletContext`.

See Also:
  - `MockServletContext`

### MockHttpSession

public MockHttpSession(@Nullable ServletContext servletContext)
Create a new MockHttpSession.

Parameters:
  `servletContext` - the ServletContext that the session runs in

### MockHttpSession

public MockHttpSession(@Nullable ServletContext servletContext, @Nullable String id)
Create a new MockHttpSession.

Parameters:
  `servletContext` - the ServletContext that the session runs in
  `id` - a unique identifier for this session

## Method Details

### getCreationTime

public long getCreationTime()

Specified by:
  `getCreationTime` in interface `HttpSession`

### getId

public String getId()

Specified by:
  `getId` in interface `HttpSession`

### changeSessionId

public String changeSessionId()
As of Servlet 3.1, the id of a session can be changed.

Returns:
  the new session id
Since:
  4.0.3

### access

public void access()

### getLastAccessedTime

public long getLastAccessedTime()

Specified by:
  `getLastAccessedTime` in interface `HttpSession`

### getServletContext

public ServletContext getServletContext()

Specified by:
  `getServletContext` in interface `HttpSession`

### setMaxInactiveInterval

public void setMaxInactiveInterval(int interval)

Specified by:
  `setMaxInactiveInterval` in interface `HttpSession`

### getMaxInactiveInterval

public int getMaxInactiveInterval()

Specified by:
  `getMaxInactiveInterval` in interface `HttpSession`

### getAttribute

@Nullable public Object getAttribute(String name)

Specified by:
  `getAttribute` in interface `HttpSession`

### getAttributeNames

public Enumeration<String> getAttributeNames()

Specified by:
  `getAttributeNames` in interface `HttpSession`

### setAttribute

public void setAttribute(String name, @Nullable Object value)

Specified by:
  `setAttribute` in interface `HttpSession`

### removeAttribute

public void removeAttribute(String name)

Specified by:
  `removeAttribute` in interface `HttpSession`

### clearAttributes

public void clearAttributes()
Clear all of this session's attributes.

### invalidate

public void invalidate()
Invalidates this session then unbinds any objects bound to it.

Specified by:
  `invalidate` in interface `HttpSession`
Throws:
  `IllegalStateException` - if this method is called on an already invalidated session

### isInvalid

public boolean isInvalid()

### setNew

public void setNew(boolean value)

### isNew

public boolean isNew()

Specified by:
  `isNew` in interface `HttpSession`

### serializeState

public Serializable serializeState()
Serialize the attributes of this session into an object that can be turned into a byte array with standard Java serialization.

Returns:
  a representation of this session's serialized state

### deserializeState

public void deserializeState(Serializable state)
Deserialize the attributes of this session from a state object created by `serializeState()`.

Parameters:
  `state` - a representation of this session's serialized state