Search in sources :

Example 11 with MockHttpSession

use of org.springframework.mock.web.test.MockHttpSession in project spring-framework by spring-projects.

the class SessionScopeTests method getFromScope.

@Test
public void getFromScope() throws Exception {
    AtomicInteger count = new AtomicInteger();
    MockHttpSession session = new MockHttpSession() {

        @Override
        public void setAttribute(String name, Object value) {
            super.setAttribute(name, value);
            count.incrementAndGet();
        }
    };
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    String name = "sessionScopedObject";
    assertNull(session.getAttribute(name));
    TestBean bean = (TestBean) this.beanFactory.getBean(name);
    assertEquals(1, count.intValue());
    assertEquals(session.getAttribute(name), bean);
    assertSame(bean, this.beanFactory.getBean(name));
    assertEquals(1, count.intValue());
    // should re-propagate updated attribute
    requestAttributes.requestCompleted();
    assertEquals(session.getAttribute(name), bean);
    assertEquals(2, count.intValue());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.test.MockHttpSession) Test(org.junit.Test)

Example 12 with MockHttpSession

use of org.springframework.mock.web.test.MockHttpSession in project spring-framework by spring-projects.

the class ServletRequestMethodArgumentResolverTests method session.

@Test
public void session() throws Exception {
    MockHttpSession session = new MockHttpSession();
    servletRequest.setSession(session);
    MethodParameter sessionParameter = new MethodParameter(method, 2);
    assertTrue("Session not supported", resolver.supportsParameter(sessionParameter));
    Object result = resolver.resolveArgument(sessionParameter, mavContainer, webRequest, null);
    assertSame("Invalid result", session, result);
    assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled());
}
Also used : MockHttpSession(org.springframework.mock.web.test.MockHttpSession) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Aggregations

MockHttpSession (org.springframework.mock.web.test.MockHttpSession)12 Test (org.junit.Test)11 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)7 HashMap (java.util.HashMap)4 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)4 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 Serializable (java.io.Serializable)1 MethodParameter (org.springframework.core.MethodParameter)1