インテグレーションテストを実行するために、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());
}
}
私のセッションはすべての要求で空ですが、なぜわかりません。
EDIT:最後のアサートは失敗しています: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