UPDATE: Spring Security 4 has added official Test Support. There is a section that describes testing with MockMvc in detail.
It sounds as though you have not added the Spring Security Filter to your MockMvc. For example:
public class MyTests {
@Autowired
private FilterChainProxy springSecurityFilterChain;
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webApplicationContextSetup(this.wac)
.addFilters(this.springSecurityFilterChain).build();
}
@Test
public void testLoginPostController() throws Exception {
Account account = new AccountBuilder("test", "test", "test@gmail.com", Address.FAKE_EMPTY_ADDRESS4TESTS)
.build();
this.mockMvc.perform(post("/j_spring_security_check").param("j_username", account.getUsername()).param("j_password", "test"))
.andDo(print())
.andExpect(status().isMovedTemporarily())
.andExpect(view().name("redirect:/public/index.htm"));
}
}
The reason this is happening is because right now the MockMvc is only aware of your Spring MVC configuration and is not aware of any Filter (i.e. the FilterChainProxy). Since validation of the username and password (i.e. processing of /j_spring_security_check) occurs within FilterChainProxy before it is sent to Spring MVC and you have not included it you are getting a 404.