Tôi đang sử dụng spring-test mới trong phiên bản 3.1 để chạy thử nghiệm tích hợp. Nó hoạt động thực sự tốt nhưng tôi không thể làm cho phiên làm việc. Mã của tôi:Kiểm tra tích hợp mvc mùa xuân 3.1 với hỗ trợ phiên
@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());
}
}
Phiên của tôi trống trong mọi yêu cầu, tôi không biết tại sao.
EDIT: Xác nhận cuối cùng bị lỗi: andExpect(status().isOk());
. Nó trả về 403 thay vì 200.
Xác nhận nào bị lỗi? –
Điều cuối cùng: 'andExpect (status(). IsOk());' bởi vì tôi kiểm tra phiên cho một biến cần được đặt, nhưng phiên đó trống nên nó sẽ trả về bị cấm. – islon
Xem thêm [Cách đăng nhập người dùng với mùa xuân 3.2 thử nghiệm mvc mới] (http://stackoverflow.com/questions/14308341/how-to-login-a-user-with-spring-3-2-new-mvc thử nghiệm). – Arjan