Test result is not as expected

Hey I have service that throws exception when user is not found, but when I am trying to test method it doesnt work as expected, it works when I test in postman and the message is right with 400 error, but test doesnt seem to work..

    @Test
    @WithMockUser(username = "admin", authorities = {"ADMIN"})
    public void shoulThrowExceptionGettingUser() throws Exception {
        doThrow(new ResponseStatusException(HttpStatus.NOT_FOUND))
                .when(userService).getUser("TEST");

        MvcResult s = mvc.perform(get("/api/user")
                .contentType(MediaType.APPLICATION_JSON)
                .param("username","Test")).andDo(print())
                .andExpect(status().isBadRequest()).andReturn();
    }
  public UserDtoNoPass getUser(String userName)
    {
        try {
            UserDetails userDetails= manager.loadUserByUsername(userName);
            UserDtoNoPass toReturn= new UserDtoNoPass();
            toReturn.setUserName(userDetails.getUsername());
            toReturn.setList(userDetails.getAuthorities().stream().map(s->s.toString()).map(
                    s->Enum.valueOf(Authority.class,s)).collect(Collectors.toList()));
            return toReturn;
        }
        catch (UsernameNotFoundException e)
        {
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
        }
    }
  @GetMapping
    public ResponseEntity<Object> getUser(@RequestParam("username") String username)
    {
        HttpHeaders headers= new HttpHeaders();
        try{
         UserDtoNoPass dto=userService.getUser(username);
         headers.add("successfull","true");
         return new ResponseEntity<>(dto,headers, HttpStatus.OK);
        }
        catch (Exception e)
        {
            headers.add("successfull","false");
            return new ResponseEntity<>("No such a user found",headers, HttpStatus.BAD_REQUEST);
        }
    }

Answer

The mock is set up for a String with value "TEST":

doThrow(new ResponseStatusException(HttpStatus.NOT_FOUND))
        .when(userService).getUser("TEST");

but the actual call uses a String with value "Test":

MvcResult s = mvc.perform(get("/api/user")
        ...
        .param("username","Test")
        ...

Both Strings should have the same value. I personally like to keep mock setup and mock validation separate. Thus I would advise to rewrite the test to:

doThrow(new ResponseStatusException(HttpStatus.NOT_FOUND))
        .when(userService).getUser(anyString());
...
MvcResult s = mvc.perform(get("/api/user")
        ...
        .param("username","Test")
        ...
...
verify(userService).getUser("Test");

As an aside: in genereal, the mock setup should use the form

when(...).then(Return|Throw)(...)

instead of

do(Return|Throw)(...).when(...)...

since the former guarantees type safety. The latter should only be used if not possible otherwise (e.g. throwing an Exception on a void method).

With this in mind, we could rewrite the mock setup to

when(userService.getUser(anyString()))
        .thenThrow(new ResponseStatusException(HttpStatus.NOT_FOUND));