통합 테스트를 실행하려면 3.1 버전의 새로운 spring-test을 사용하고 있습니다. 그것은 잘 작동하지만 세션을 작동시킬 수는 없습니다. 내 코드 :Spring mvc 3.1 세션 지원이 포함 된 통합 테스트
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration("src/main/webapp")
@ContextConfiguration({"classpath:applicationContext-dataSource.xml",
"classpath:applicationContext.xml",
"classpath:applicationContext-security-roles.xml",
"classpath:applicationContext-security-web.xml",
"classpath:applicationContext-web.xml"})
public class SpringTestBase {
@Autowired
private WebApplicationContext wac;
@Autowired
private FilterChainProxy springSecurityFilterChain;
@Autowired
private SessionFactory sessionFactory;
protected MockMvc mock;
protected MockHttpSession mockSession;
@Before
public void setUp() throws Exception {
initDataSources("dataSource.properties");
mock = MockMvcBuilders.webAppContextSetup(wac).addFilters(springSecurityFilterChain).build();
mockSession = new MockHttpSession(wac.getServletContext(), UUID.randomUUID().toString());
}
@Test
public void testLogin() throws Exception {
// this controller sets a variable in the session
mock.perform(get("/")
.session(mockSession))
.andExpect(model().attributeExists("csrf"));
// I set another variable here just to be sure
mockSession.setAttribute(CSRFHandlerInterceptor.CSRF, csrf);
// this call returns 403 instead of 200 because the session is empty...
mock.perform(post("/setup/language")
.session(mockSession)
.param(CSRFHandlerInterceptor.CSRF, csrf)
.param("language", "de"))
.andExpect(status().isOk());
}
}
요청할 때마다 내 세션이 비어 있습니다. 이유를 모르겠습니다.
편집 : 마지막 어설 션이 실패했습니다 : andExpect(status().isOk());
. 그것은 200 대신에 403을 반환합니다.
어설 션 중 실패한 것은 무엇입니까? –
마지막 하나 :'andExpect (status(). isOk());'내가 설정해야하는 변수에 대한 세션을 확인했기 때문에 세션이 비어있어 금지 된 상태로 되돌아갑니다. – islon
참고 [봄 3.2 새로운 mvc 테스트로 사용자 로그인하는 법] (http://stackoverflow.com/questions/14308341/how-to-login-a-user-with-spring-3-2-new-mvc 테스트). – Arjan