title: Spring Boot tests (MockMvc) - received HTTP status code 400, expecting 200
body: <p>Could somebody tell me what is wrong in the following test?</p>
  <p><strong><code>shouldThrowNotAuthorizedException</code></strong> works fine, but the test for <strong><code>shouldCreateUser</code></strong> returns a <strong>HTTP status code 400</strong>.</p>
  <p>UserControllerTest:</p>
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @WebMvcTest(UserController.class)
  public class UserControllerTest extends BaseControllerTest {
  
  @Autowired
  private MockMvc mvc;
  
  @MockBean
  private UserService userService;
  
  
  @Before
  public void setUp(){
      mvc = MockMvcBuilders.standaloneSetup(UserController.class)
              .setControllerAdvice(new ExceptionControllerAdvice())
              .build();
  }
  
  @Test
  public void shouldThrowNotAuthorizedException () throws Exception {
  
    UserCredentialsDTO userCredentialsDTO = createRandomUserCredentialsDTO();
    Gson gson = getGsonInstance();
  
     given(this.userService.isAuthorized(userCredentialsDTO)).willThrow(NotAuthorizedException.class);
  
     this.mvc.perform(MockMvcRequestBuilders.get(&quot;/user/authorize&quot;)
             .accept(MediaType.APPLICATION_JSON)
             .content(gson.toJson(userCredentialsDTO)))
     .andExpect(MockMvcResultMatchers.status().is4xxClientError());
  }
  
  @Test
  public void shouldCreateUser() throws Exception {
  
      Gson gson = getGsonInstance();
      UserCredentialsDTO userCredentialsDTO =  createRandomUserCredentialsDTO();
  
       given(this.userService.create(userCredentialsDTO)).willReturn(ResponseEntity.ok(userCredentialsDTO));
  
     this.mvc.perform(MockMvcRequestBuilders.post(&quot;/user&quot;)
              .accept(MediaType.APPLICATION_JSON)
              .content(gson.toJson(userCredentialsDTO)))
             .andExpect(MockMvcResultMatchers.status().is2xxSuccessful())
             .andExpect(MockMvcResultMatchers.content().json(gson.toJson(userCredentialsDTO)));
   }
  
  private SuperUser createSuperUser(UserCredentialsDTO userCredentialsDTO) {
      SuperUser user = new SuperUser();
      user.setUserName(userCredentialsDTO.getUserName());
      user.setPassword(userCredentialsDTO.getPassword());
      return user;
  }
  
  private UserCredentialsDTO createRandomUserCredentialsDTO() {
      UserCredentialsDTO userCredentialsDTO = new UserCredentialsDTO();
      userCredentialsDTO.setUserName(&quot;Test&quot;);
      userCredentialsDTO.setPassword(&quot;GGGaaa@@4&quot;);
      userCredentialsDTO.setUserRole(UserRole.CUSTOMER);
      return userCredentialsDTO;
   }
  
  
  
  }
  </code></pre>
  <p>UserController:</p>
  <pre><code>@RestController
  @RequestMapping(value = &quot;/user&quot;)
  public class  UserController {
  
  @Autowired
  private UserService userService;
  
  @RequestMapping(method = RequestMethod.POST)
  public ResponseEntity&lt;UserCredentialsDTO&gt; createUser(@RequestBody  UserCredentialsDTO userCredentialsDTO) {
      return userService.create(userCredentialsDTO);
  }
  
  @RequestMapping(value = &quot;/authorize&quot;, method = RequestMethod.GET)
  public ResponseEntity&lt;UserCredentialsDTO&gt; authorizeUser(@RequestBody UserCredentialsDTO userCredentialsDTO) {
      return userService.isAuthorized(userCredentialsDTO);
  }
  </code></pre>
  <p>UserServiceImpl:</p>
  <pre><code>@Override
  public ResponseEntity&lt;UserCredentialsDTO&gt; create(UserCredentialsDTO userCredentialsDTO) {
  
      User user = null;
  
      switch (userCredentialsDTO.getUserRole()) {
          case SUPER_USER:
              user = new SuperUser();
              break;
          case CUSTOMER:
              user = new Customer();
              break;
      }
  
      user.setPassword(userCredentialsDTO.getPassword());
      user.setUserName(userCredentialsDTO.getUserName());
  
      userRepository.save(user);
  
      return ResponseEntity.ok(userCredentialsDTO);
  }
  </code></pre>
  <p>Exception</p>
  <pre><code>java.lang.AssertionError: Range for response status value 404 
  Expected :SUCCESSFUL
  Actual   :CLIENT_ERROR
  </code></pre>
  <p>What's wrong with this test?</p>
  <p>Stacktrace:</p>
  <pre><code>java.lang.AssertionError: Range for response status value 404 
  Expected :SUCCESSFUL
  Actual   :CLIENT_ERROR
  &lt;Click to see difference&gt;
  
  
  at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54)
  at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81)
  at org.springframework.test.web.servlet.result.StatusResultMatchers$4.match(StatusResultMatchers.java:93)
  at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171)
  at com.shoponline.controller.UserControllerTest.shouldCreateUser(UserControllerTest.java:75)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
  at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
  at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
  at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
  at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
  at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
  at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
  at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
  </code></pre>
  <p>Also I noticed some warnings in logs:</p>
  <pre><code>     main] o.s.web.servlet.PageNotFound             : No mapping found for  HTTP request with URI [/user/authorize] in DispatcherServlet with name ''
  2017-01-23 22:03:22.955  INFO 1868 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.test.web.servlet.setup.StubWebApplicationContext@359b650b
  2017-01-23 22:03:22.955  INFO 1868 --- [           main] .m.m.a.ExceptionHandlerExceptionResolver : Detected @ExceptionHandler methods in com.shoponline.controller.ExceptionControllerAdvice#6a9b0a6f
  2017-01-23 22:03:22.955  INFO 1868 --- [           main] o.s.mock.web.MockServletContext          : Initializing Spring FrameworkServlet ''
  2017-01-23 22:03:22.955  INFO 1868 --- [           main] o.s.t.web.servlet.TestDispatcherServlet  : FrameworkServlet '': initialization started
  2017-01-23 22:03:22.965  INFO 1868 --- [           main] o.s.t.web.servlet.TestDispatcherServlet  : FrameworkServlet '': initialization completed in 10 ms
  2017-01-23 22:03:22.965  WARN 1868 --- [           main] o.s.web.servlet.PageNotFound             : No mapping found for HTTP request with URI [/user] in DispatcherServlet with name ''
  </code></pre>