### Question:
DummyAuthenticationProvider extends DaoAuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) { UsernamePasswordAuthenticationToken usernamePasswordAuthentication; try { usernamePasswordAuthentication = (UsernamePasswordAuthenticationToken) super.authenticate(authentication); } catch (AuthenticationException exception) { throw new BadCredentialsException("Username or password are invalid."); } catch (Exception e) { throw new AuthenticationServiceException("A failure occurred when authenticating.", e); } String username = usernamePasswordAuthentication.getName(); String token = authenticationService.createJwtToken(username, DUMMY_PROVIDER, null); TokenAuthentication tokenAuthentication = new TokenAuthentication(username, token); tokenAuthentication.setAuthenticated(true); return tokenAuthentication; } DummyAuthenticationProvider(BCryptPasswordEncoder encoder,
                                       @Qualifier("dummyService") UserDetailsService userDetailsService,
                                       AuthenticationService authenticationService); @Override Authentication authenticate(Authentication authentication);  }### Answer:
@Test void shouldReturnDummyToken() { UsernamePasswordAuthenticationToken usernamePasswordAuthentication = new UsernamePasswordAuthenticationToken(PRINCIPAL, USERNAME); Authentication returnedTokenAuthentication = dummyAuthenticationProvider.authenticate(usernamePasswordAuthentication); assertTrue(returnedTokenAuthentication.isAuthenticated()); assertEquals(USERNAME, returnedTokenAuthentication.getName()); assertEquals(PRINCIPAL, returnedTokenAuthentication.getPrincipal()); }
@Test void shouldThrowExceptionIfTokenNotValid() { Exception exception = assertThrows(AuthenticationServiceException.class, () -> dummyAuthenticationProvider.authenticate(null), "Expected exception is not AuthenticationServiceException"); assertEquals("A failure occurred when authenticating.", exception.getMessage()); }
@Test void shouldThrowExceptionIfCredentialsAreNull() { UsernamePasswordAuthenticationToken usernamePasswordAuthentication = new UsernamePasswordAuthenticationToken(PRINCIPAL, "sdsd"); Exception exception = assertThrows(BadCredentialsException.class, () -> dummyAuthenticationProvider.authenticate(usernamePasswordAuthentication), "Expected exception is not BadCredentialsException"); assertEquals("Username or password are invalid.", exception.getMessage()); }