### Question:
AuthenticationFilter extends UsernamePasswordAuthenticationFilter { @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) { try { UserCredentials credentials = getCredentials(request); UsernamePasswordAuthenticationToken token = createAuthenticationToken(credentials); return authenticationManager.authenticate(token); } catch (IOException e) { throw new UnreadableCredentialsException("Credentials could not be read", e); } } AuthenticationFilter(AuthenticationManager authenticationManager, TokenProperties tokenProperties,
                                ObjectMapper objectMapper, Clock clock); @Override Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response);  }### Answer:
@Test void shouldAuthenticate() throws IOException { var credentials = new UserCredentials("user", "test", "role"); when(request.getInputStream()) .thenReturn(servletInputStream); when(objectMapper.readValue(servletInputStream, UserCredentials.class)) .thenReturn(credentials); var token = new UsernamePasswordAuthenticationToken( credentials.getUsername(), credentials.getPassword(), emptyList() ); when(authenticationManager.authenticate(token)) .thenReturn(authentication); assertNotNull(filter.attemptAuthentication(request, response)); }
@Test void shouldThrowExceptionWhenCantReadCredentials() throws IOException { when(request.getInputStream()) .thenReturn(servletInputStream); when(objectMapper.readValue(servletInputStream, UserCredentials.class)) .thenThrow(IOException.class); assertThrows(UnreadableCredentialsException.class, () -> filter.attemptAuthentication(request, response)); }