Unit-Tests /login in Spring MVC mit MockMvc

Habe ich eine sehr einfache REST-Anwendung erstellt, mit Spring MVC. (Code steht bei GitHub.) Es hat eine einfache WebSecurityConfigurer wie folgt:

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity
            .csrf().disable()
            .exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint)
                .and()
            .authorizeRequests()
                .antMatchers("/user/new").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login").permitAll()
                .successHandler(authenticationSuccessHandler)
                .failureHandler(authenticationFailureHandler)
                .and()
            .logout()
                .permitAll()
                .logoutSuccessHandler(logoutSuccessHandler);
}

Wenn ich die Anwendung ausführen, werden sowohl die custom-Controller und die login/logout-Seiten arbeiten, ohne ein problem. Ich kann auch unit-test /user/new über MockMvc. Jedoch, wenn ich versuche zu testen /login mit der folgenden Funktion

@Test
public void testUserLogin() throws Exception {
    RequestBuilder requestBuilder = post("/login")
            .param("username", testUser.getUsername())
            .param("password", testUser.getPassword());
    mockMvc.perform(requestBuilder)
            .andDo(print())
            .andExpect(status().isOk())
            .andExpect(cookie().exists("JSESSIONID"));
}

scheitert es wie folgt:

MockHttpServletRequest:
         HTTP Method = POST
         Request URI = /login
          Parameters = {username=[test-user-UserControllerTest], password=[test-user-UserControllerTest-password]}
             Headers = {}

             Handler:
                Type = org.springframework.web.servlet.resource.ResourceHttpRequestHandler

               Async:
   Was async started = false
        Async result = null

  Resolved Exception:
                Type = org.springframework.web.HttpRequestMethodNotSupportedException

        ModelAndView:
           View name = null
                View = null
               Model = null

            FlashMap:

MockHttpServletResponse:
              Status = 405
       Error message = Request method 'POST' not supported
             Headers = {Allow=[HEAD, GET]}
        Content type = null
                Body = 
       Forwarded URL = null
      Redirected URL = null
             Cookies = []

java.lang.AssertionError: Status expected:<200> but was:<405>

Aber ich bin mir ziemlich Benutzer POST zu /login ist zu arbeiten, wenn ich die Anwendung ausführen, anstatt zu testen. Weiter, wenn ich versuche, eine GET oder HEAD Anfrage (wie vorgeschlagen in der Headers = {Allow=[HEAD, GET]} Zeile des logs), diesmal erhalte ich einen 404. Irgendwelche Ideen was Los ist und wie ich es beheben kann?

  • Ich glaube /login-URL erfolgt über spring security filter, und ich bin nicht sicher, ob Sie noch "konfiguriert", es während Ihres test-setup
Schreibe einen Kommentar