title: Unit Testing /login in Spring MVC using MockMvc
body: <p>I have a very simple REST application created using Spring MVC. (Code is available at <a href="https://github.com/tanidiktan/backend" rel="noreferrer">GitHub</a>.) It has a simple <a href="https://github.com/tanidiktan/backend/blob/master/src/main/java/com/tanidiktan/backend/config/WebSecurityConfiguration.java" rel="noreferrer"><code>WebSecurityConfigurer</code></a> as follows:</p>
  
  <pre><code>@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);
  }
  </code></pre>
  
  <p>When I run the application, both the custom controllers and the login/logout pages work without a problem. I can even <a href="https://github.com/tanidiktan/backend/blob/master/src/test/java/com/tanidiktan/backend/controller/UserControllerTest.java" rel="noreferrer">unit test</a> <code>/user/new</code> via <code>MockMvc</code>. However, when I try to test <code>/login</code> with the following function</p>
  
  <pre><code>@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"));
  }
  </code></pre>
  
  <p>it fails as follows:</p>
  
  <pre><code>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:&lt;200&gt; but was:&lt;405&gt;
  </code></pre>
  
  <p>But I am pretty user <code>POST</code> to <code>/login</code> is working when I run the application instead of test. Further, when I try make a <code>GET</code> or <code>HEAD</code> request (as suggested in the <code>Headers = {Allow=[HEAD, GET]}</code> line of the logs), this time I receive a 404. Any ideas about what is going on and how can I fix it?</p>